Automatically calling a method

Discussion in 'iPhone/iPad Programming' started by hervey, Nov 29, 2009.

  1. macrumors newbie

    At startup my TabBarController loads a NavigationController, which has a date picker. This picker is animated up and down by two methods (pickerUp, pickerDown). All this and the formatter are functioning well.

    It would be better for the user if the picker was onscreen at startup. Using the pickerUp method - called automatically on loading - would be a good solution.

    I'm new to cocoa and could use some guidance.
  2. macrumors demi-god

    You can place the method within a method that's called when the view is loaded. You can uncomment the method called viewDidLoad within your view controller and place it there. Once that specific view controller is loaded this method is called and will then call whatever method you placed inside.

    Hope this makes sense.


    - (void)viewDidLoad {
        [super viewDidLoad];
        // Place the method you want called here
       [self myMethod];
  3. macrumors newbie

    Thanks for your reply, I hope you can still help me understand how to call this method.

    I have tried the following (separately) - none of the three would allow the program to launch. The method I'm trying to call is at the bottom. I could cut and paste the code here, but it's a lot of code (to me) and would require some scrolling.

    Reading the docs, viewDidLoad seemed the logical choice, but I'm unable to write it correctly. I did get the picker up at launch using the applicationDidFinishlauncing method, but it duplicated the picker and I couldn't connect it to the pickerDown or formatter methods.

    - (void)viewDidLoad {
     [super viewDidLoad];
     1. [self pickerUp];
     2. [self pickerUp:];
     3. [self pickerUp:(id)sender];
    - (IBAction)pickerUp:(id)sender { }
  4. Moderator emeritus


    [self pickerUp:nil];
  5. macrumors newbie


    [self pickerUp:nil];
    in viewDidLoad called the pickerUp method. The logs verify the call and the completion of the method - but the picker still does not show.

    The control that normally calls the picker, a UIButton with 0.0 alpha superimposed over the textfield that displays the product of the picker (date/time) still functions normally: pickerUp and dateFormatter.

    'Tis a mystery to me.
  6. Moderator


    Staff Member

    Try viewDidAppear instead.
  7. macrumors newbie

    Thanks, but the picker remains out of sight, the pickerUp method was not called. The UIButton still brings the picker on screen with the pickerUp method, which is copied below:

    -(void)viewDidAppear { ///3
    	[self pickerUp:nil];
    	NSLog(@"picker did appear");
    - (IBAction)pickerUp:(id)sender {
    	if (self.datePicker.superview == nil) {
    		datePicker = [[UIDatePicker alloc]init] ;
    		datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    		datePicker.minuteInterval = 05;
    		[datePicker addTarget:self action:@selector (formatPickerDate:)forControlEvents:UIControlEventValueChanged];
    		[self.view.window addSubview: self.datePicker];
    		CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    		CGSize pickerSize = [self.datePicker sizeThatFits:CGSizeZero];
    		CGRect startRect = CGRectMake(0.0,screenRect.origin.y + screenRect.size.height,pickerSize.width, pickerSize.height);
    		self.datePicker.frame = startRect;
    		CGRect pickerRect = CGRectMake(0.0,screenRect.origin.y + screenRect.size.height - pickerSize.height, pickerSize.width, pickerSize.height);
    		[UIView beginAnimations:nil context:NULL];
    		[UIView setAnimationDuration:0.3];
    		[UIView setAnimationDelegate:self];
    		self.datePicker.frame = pickerRect;
    		[UIView commitAnimations];
    	NSLog(@"pickerUp ended");
  8. Moderator


    Staff Member

    You need to make sure you are defining viewDidAppear so that it matches UIViewController's viewDidAppear: instance method, which the documentation shows as:
    - (void)viewDidAppear:(BOOL)animated
  9. macrumors newbie

    YES, YES! The following code called the pickerUp method when the viewController was on screen (window).

    - (void)viewDidAppear:(BOOL)animated {
    	[self pickerUp:nil];
    	NSLog(@"picker was called ");
    Thank all of you for your guidance.


Share This Page