PDA

View Full Version : Automatically calling a method




hervey
Nov 29, 2009, 09:30 PM
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.



ApplEngineer
Nov 29, 2009, 09:45 PM
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:


- (void)viewDidLoad {
[super viewDidLoad];
// Place the method you want called here

[self myMethod];
}

hervey
Nov 30, 2009, 08:07 PM
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 { }

kainjow
Nov 30, 2009, 08:58 PM
Try
[self pickerUp:nil];

hervey
Dec 1, 2009, 07:35 PM
Yes!

[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.

dejo
Dec 1, 2009, 07:49 PM
[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.
Try viewDidAppear instead.

hervey
Dec 1, 2009, 08:54 PM
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 {
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");
}

dejo
Dec 2, 2009, 10:26 AM
-(void)viewDidAppear { ///3
[self pickerUp:nil];
NSLog(@"picker did appear");
}

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

hervey
Dec 5, 2009, 07:19 PM
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.

Cheers