hamobi
hamobi

Reputation: 8130

iOS SDK: about using properties when creating objects programmatically?

okay below is a standard example of creating a datepicker

- (void)viewDidLoad {

    CGRect pickerFrame = CGRectMake(0,250,100,100);

    UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:myPicker];
    [myPicker release]; 
}

- (void)pickerChanged:(id)sender
{
    NSLog(@"value: %@",[sender date]);
}

this is all good and well. I'm a little used to creating elements in IB so when I create an object programatically I'm not sure how to access the data.

What I mean is.. should I assign myPicker to a class property and then access it as _myPicker?

Or lets say I want to access the date inside of the pickerChanged method without calling another method. Should I assign an NSDate property and re-assign it every time the picker is changed?

I ran into some memory issues when I was trying to do it that way. I had another method grabbing _theDate, and it probably tried to access it at the same time pickerChanged was modifying it?

Anyway, what I'm getting at is "whats the proper workflow when creating things like action sheets, and pickers programmatically". When these things are changed, how should the resulting data be saved so the rest of the class can access it?

Bonus question:

Is there a difference between this?

for(UILabel *myLabel in view.subviews){
    NSLog(myLabel.text);
}

and this? Do I need to check the class all the time if i know my view only contains a certain kind of object?

for((id) myLabel in view.subviews){
    if([myLabel isKindOfClass:[UILabel class]){
        UILabel *theLabel = myLabel;
        NSLog(myLabel.text);
    }
}

Upvotes: 0

Views: 636

Answers (3)

Rajeev Barnwal
Rajeev Barnwal

Reputation: 1347

You need to declare a UIDatePicker property to hold one instance of your child controller

This is what you need to add in your .h file:

@property (strong, nonatomic) UIDatePicker *myPicker;

And then in your .m file you need to add a data source method for this date picker. something like what rdelmar has instructed above:

self.myPicker = [[UIDatePicker alloc] init];

Upvotes: 0

Bay Phillips
Bay Phillips

Reputation: 2045

Generally, you will just define properties if you'll need to access them more than once. You can do this in the .m file's interface:

@interface MyObject()

@property (weak, nonatomic) UIDatePicker *myPicker;

@end

You will then be able to access it by either _myPicker or self.myPicker.

You shouldn't need another NSDate property in your class because you can access the set date at any time:

_myPicker.date

For your last question: the latter of the two is merely extra sanity checks. While you're writing your own code, and you should know what subviews you're adding in, it can't hurt to double check the type of the subviews incase anything should go wrong and you try to access selectors that don't exist. This is a larger programming question though and not necessarily objective-c or iOS specific.

Upvotes: 1

swampf0etus
swampf0etus

Reputation: 411

The documented approach is to intercept the UIControlEventValueChanged event, as per your example.

You would then typically copy the [sender date] value to a property in your pickerChanged: method.

If the user hits a save button, then the object that presented the view containing the picker should be able to retrieve the selected date via the property.

It's not considered good practice to use isKindOfClass:. You should structure your code such that you always know what class you're dealing with.

Also, you should really switch to ARC so you don't need to worry about calling release

Upvotes: 0

Related Questions