Automatically calling a method

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

  1. macrumors newbie

    Joined:
    Sep 23, 2009
    #1
    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

    Joined:
    Jul 7, 2007
    #2
    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.

    Example:

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

    Joined:
    Sep 23, 2009
    #3
    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.

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

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Try
    Code:
    [self pickerUp:nil];
     
  5. thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #5
    Yes!

    Code:
    [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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Try viewDidAppear instead.
     
  7. thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #7
    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:

    Code:
    -(void)viewDidAppear { ///3
    	[self pickerUp:nil];
    	NSLog(@"picker did appear");
    }
    
    - (IBAction)pickerUp:(id)sender {
    	NSLog(@"pickerUp");
    	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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You need to make sure you are defining viewDidAppear so that it matches UIViewController's viewDidAppear: instance method, which the documentation shows as:
    Code:
    - (void)viewDidAppear:(BOOL)animated
     
  9. thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #9
    YES, YES! The following code called the pickerUp method when the viewController was on screen (window).

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

    Cheers
     

Share This Page