Τετάρτη, 23 Ιανουαρίου 2013

How to the keyboard NOT slides up overtop of a textfield

1. Add UITextFieldDelegate delegate at .h file
2. Connect from xib file your textField with file's Owner as Delegate
3.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = textField.frame.origin.y / 2; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed
    
    int movement = (up ? -movementDistance : movementDistance);
    
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

the solution from stackoverflow here.

but here a comment on that
I have to mention that "If your are writing an application for iOS 4 or later, you should use the block-based methods for animating your content instead." Referenced from :developer.apple.com/library/ios/#documentation/windowsviews/…

Τρίτη, 22 Ιανουαρίου 2013

ios pickerView - How to add one pickerview for many textfields

basic thinks:
You must follow the pickerView delegate.
You need to add pickerView with data similar to tablerow delegate.
You can add pickerView.view as inputView of one textfield.
You adding a Toolbar with a done button

1. Create pickerView at xib. Place it outside of the main view and connect its Delegates and source with ctrl+mouse with your File's Owner. Also make a property of him.

2. at .h file of your viewController

@class PKAPeople;
@interface ApartmentsDetailViewController : UIViewController <UITabBarDelegate, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>{
    NSMutableArray * pickerViewData;
}

3. At viewDidLoad make the data source of your pickerview
pickerViewData   = [[NSMutableArray alloc]init];
    for (int i=0;i<=1000;i++)
        [pickerViewData addObject:[[NSString alloc]initWithFormat:@"%i",i]];

4. Make the pickerView inputView of yours textfields
self.xiliostaKoinoxriston.inputView = self.pickerView;
    self.xiliostaAnsanser.inputView = self.pickerView;
    self.xiliostaAllo.inputView = self.pickerView;

5. Create the toolbar for aesthetics and control management. Be aware to make the proper selector below methode if you don't want crash. Also note the CGRectMake is not variables and in this case doesn't support view rotation.  
   UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
    CGRectMake(0,0, 320, 44)]; //should code with variables to support view rotation
    UIBarButtonItem *doneButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                  target:self action:@selector(patithikeDoneStoPickerView)];
    [myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];


6. add the toolbar at yours textViews
self.xiliostaKoinoxriston.inputAccessoryView = myToolbar;
self.xiliostaAnsanser.inputAccessoryView = myToolbar;
self.xiliostaAllo.inputAccessoryView = myToolbar;

7. Implement the pickerView protocol
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
     return [pickerViewData count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [[NSString alloc] initWithFormat:@"%@",[pickerViewData objectAtIndex:row]];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
       if (self.xiliostaKoinoxriston.isFirstResponder)
        self.xiliostaKoinoxriston.text = [pickerViewData objectAtIndex:row];
    if (self.xiliostaAnsanser.isFirstResponder)
        self.xiliostaAnsanser.text = [pickerViewData objectAtIndex:row];
    if (self.xiliostaAllo.isFirstResponder)
        self.xiliostaAllo.text = [pickerViewData objectAtIndex:row];
}

8. Implement the done button selection method so to resign the "modular" keyboard you just made. Note that i search WHO is the firstresponder and HIM resigns. Otherwise a pickerView will stuck on you screen :) 
- (void) patithikeDoneStoPickerView
{
    if (self.xiliostaKoinoxriston.isFirstResponder)
        [self.xiliostaKoinoxriston resignFirstResponder];
    if (self.xiliostaAnsanser.isFirstResponder)
        [self.xiliostaAnsanser resignFirstResponder];
    if (self.xiliostaAllo.isFirstResponder)
        [self.xiliostaAllo resignFirstResponder];
    
}



Πέμπτη, 17 Ιανουαρίου 2013

how to make pdf with IOS

full instructions here

but keep on mind for ios6.

use this

CFStringRef stringRef = (__bridge_retained CFStringRef)textToDraw;

instead of this
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;



and your application will not crash :))

Τετάρτη, 16 Ιανουαρίου 2013

How to add nil into Array

You can add nil with addObject into Arrays.

Instead you can add the [NSNull null]  object.

[myArray addObject: [NSNull null]];

after that
[checkThisObject isEqual:[NSNull null]];


Δευτέρα, 14 Ιανουαρίου 2013

HOW TO SEND EMAIL FROM YOUR APPLICATION

full and best instrunctions here

synoptic:
on .h file add:

  • #import <MessageUI/MessageUI.h>
  • <MFMailComposeViewControllerDelegate>
- (void) openMail;
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;

  • connect the openMail with an action in any way you want

on .m file add

- (void) openMail {
    
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"balblabla"];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"blablabla@gmail.com", nil];
       [mailer setToRecipients:toRecipients];
        NSString *emailBody = @"";
        [mailer setMessageBody:emailBody isHTML:NO];
        //Deprecated over ios 6.0 [self presentModalViewController:mailer animated:YES];
        [self presentViewController:mailer animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        
    }
}




#pragma mark - Email Delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    // Remove the mail view
   //deprecated from 6.0  [self dismissModalViewControllerAnimated:YES];
  [self dismissViewControllerAnimated:YES completion:nil];
}

ios - play a click sound when button pressed


1. insert frameworks (MediaPlayer, AVFoundation, AudioToolbox)

2. in .h file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@interface PKAPliromesViewController : UIViewController <AVAudioPlayerDelegate>{...}

- (void) playSoundwithFileName: (NSString *) filename  fileType:(NSString *) fileType;


3. in. m file
- (void) playSoundwithFileName: (NSString *) filename  fileType:(NSString *) fileType
{
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(dispatchQueue, ^(void)
                   {
                       NSBundle * mainBundle = [NSBundle mainBundle];
                       NSString * filePath = [mainBundle pathForResource:filename ofType:fileType];
                       NSData * fileData = [NSData dataWithContentsOfFile:filePath];
                       NSError *error = nil;
                       
                       //start the audio player
                       self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
                       
                       /* did we bet an instance of AVAudioPlayer? */
                       if (self.audioPlayer != nil){
                           //set the delegate and start playing
                           self.audioPlayer.delegate = self;
                           if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play])
                           {
                               //sucessfully started play
                           }
                           else{
                               //failed to play
                           }
                       }
                       else
                       {
                           //failed to instatitate avaudioplayer;
                       }
                   }
                   ); //END DISPATCH_ASYNC
    
    

}