ページ

2011年6月2日木曜日

Xcode pickerでitemを選択する





pickerViewController.h
#import <UIKit/UIKit.h>

@interface pickerViewController : UIViewController  {

 UIPickerView *myPicker;
 NSArray *pickerData;
}

@property (nonatomic, retain) IBOutlet UIPickerView *myPicker;
@property (nonatomic, retain) IBOutlet NSArray *pickerData;

-(IBAction) buttonPressed;

@end


pickerViewController.m
#import "pickerViewController.h"

@implementation pickerViewController

@synthesize myPicker;
@synthesize pickerData;

-(IBAction) buttonPressed{

 NSInteger row = [myPicker selectedRowInComponent:0];
 NSString *selected = [pickerData objectAtIndex:row];
 
 NSString *title = [[NSString alloc] initWithFormat:@"You selected %@!", selected];
 
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
             message:@"Thank you for choosing" 
               delegate:nil 
            cancelButtonTitle:@"Cansel" 
            otherButtonTitles:@"Buy Now", nil];
 [alert show];
 [alert release];
 [title release];
 
}


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

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

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

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 NSArray *array = [[NSArray alloc] initWithObjects:@"Cheetah", @"Puma", @"Jaguar", @"Panther", @"Tiger", @"Leopard", 
       @"Snow Leopard", @"Lion", nil];
 self.pickerData = array;
 
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
 
 self.myPicker = nil;
 self.pickerData = nil;
 
 [super viewDidUnload];
}


- (void)dealloc {
    [super dealloc];
}

@end


参考


學寫iPhone程式 第94堂 - 開始 Single Picker
http://www.youtube.com/watch?v=OqFP3Rt8L8I&feature=related&hd=1

YouTube - 學寫iPhone程式 第95堂 - 繼續Single Picker
http://www.youtube.com/watch?v=IcVG1EbJmaI&feature=related&hd=1

YouTube - 學寫iPhone程式 第96堂 - 完成Single Picker
http://www.youtube.com/watch?v=eSrSCpd21Kk&feature=related&hd=1


0 件のコメント:

コメントを投稿