Thursday, 17 January 2013

To go back to the root controller when using navigation.

      [self.view removeFromSuperview];

Note: call above method from viewDidLoad method like    :   [self call];

To hide keypad when touching on the screen in iOS

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    [self.view endEditing:YES];

To hide keypad when touching return button in iOS

- (BOOL)textFieldShouldReturn:(UITextField *)textField
    [textField resignFirstResponder];
    return YES;

Note: Remember to add uitextfielddelegate in interface 

To pop up a alert box in iOS

UIAlertView *Alert1 = [[UIAlertView alloc]
                              initWithTitle:@"Complete all fields" message:@" All fields are mandatory" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        // Display alert box
        [Alert1 show];

To create button dynamically and sending message .

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
    [button setTitle:@"SUBMIT" forState:UIControlStateNormal];
    button.frame = CGRectMake(100.0, 210.0, 100, 40.0);
    [self.view addSubview:button]; 

Note: When we call a method in IOS we say sending message not calling. because we we can send  message either objects are valid or not .. it may crash when user clicked on the button if calling method is not present but it means you can write methods name even though they are not presents.

To create dynamically label

    UILabel *label  =  [[UILabel alloc]init];
    label.frame     =  CGRectMake(50,125,100,20);

    [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:18]];
    label.textColor=[UIColor whiteColor];
    label.backgroundColor=[UIColor clearColor];
    label.text      =  @"label";
    [self.view addSubview: label];

To create dynamically textfield

UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(155, 100, 100, 20)];
    nameField.returnKeyType = UIReturnKeyDone;
    [nameField setKeyboardType:UIKeyboardTypeNamePhonePad];
    nameField.backgroundColor=[UIColor whiteColor];
    nameField.text = @"";
    nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    nameField.adjustsFontSizeToFitWidth = TRUE;
    [self.view addSubview:nameField];