iPhone UIPickerView in UIViewController

雨燕双飞 提交于 2019-12-12 19:07:48

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/2250063/iphone-uipickerview-in-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!