开发者

iPhone UIPickerView in UIViewController

开发者 https://www.devze.com 2022-12-20 10:52 出处:网络
I have a xib in which I have added a UIViewController named delta. The view under delta is controlled by the delta viewcontroller, not the file owner. On the delta view, I have a UIViewPicker. My issu

I have a xib in which I have added a UIViewController named delta. The view under delta is controlled by the delta viewcontroller, not the file owner. On the delta view, I have a UIViewPicker. My issue is that I am programming in the UIPickerView in the deltaviewcontroller and I'm issuing deltaviewcontroller as the delegate and data source for the UIPickerView. Everything should work, but when I load the deltaviewcontroller's view, the a开发者_如何学Pythonpplication crashes. If I carry out everything the same way under the file's owner view, it works fine. I'm wondering if there is really a way to make the UIPickerView work with a UIViewController and no necessarily a file's owner.

For references, the code:

Header

@interface DeltaViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
    NSMutableArray *arrayNo;
    IBOutlet UIPickerView *pickerView;
}
@property (nonatomic, retain) UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *arrayNo;

@end

Implementation

#import "DeltaViewController.h"

@implementation DeltaViewController

@synthesize pickerView;
@synthesize arrayNo;

- (void)viewDidLoad
{
    NSMutableArray *dollarsArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < 100; i++)
    {
        NSString *item = [[NSString alloc] initWithFormat:@"%i", i];
        [dollarsArray addObject:item];
    }

    self.arrayNo = dollarsArray;

    [dollarsArray release]; 
}

#pragma mark -
#pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [arrayNo count];
}

#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [arrayNo objectAtIndex:row];
}

- (void)dealloc {
    [arrayNo release];
    [pickerView release];
    [super dealloc];
}

@end

Please point out if I'm doing anything wrong. Help is much appreciated.


If you search how to add a UIPickerView in a UIView, just check out the Apple sample code for UIPickerView. I think it's one of the best ways learning to use the different classes correctly. There are five project which includes an UIPickerView. Here's the link

UIPickerView class reference and sample code


Try doing [self.view addSubview:pickerView]; in your viewDidLoad method.

0

精彩评论

暂无评论...
验证码 换一张
取 消