我的成功结果来自NSMutableArray
数据类型的网络服务,但我的困境是如何向我的UIPickerView
对象显示内容结果?当我运行它时,UIPickerView
上没有任何内容,但是当我点击按钮时,它会从Web服务内容中弹出第一个国家/地区。任何评论将不胜感激,在这里我有我的代码:
myViewController.h
#import <UIKit/UIKit.h>
#import "Service.h"
@class myController;
@interface PromoViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UIPickerViewDelegate>{
}
@property (strong, nonatomic) IBOutlet UIPickerView *singlePicker;
@property (strong, nonatomic) NSArray *pickerData;
- (IBAction)buttonPressed;
@end
=======================
myViewController.m:
#import "myViewController.h"
#import "Service.h"
@implementation myViewController;
@synthesize singlePicker;
@synthesize pickerData;
- (IBAction)buttonPressed {
NSInteger row = [singlePicker 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:@"You're Welcome"
otherButtonTitles:nil];
[alert show];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia",
// @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
//self.pickerData = array;
Service* service = [[Service alloc] init];
[service WEBServiceCountries:self action:@selector(handleAllCountries:)];
//self.pickerData = countryArray;
}
-(NSMutableArray *) handleAllCountries:(id) value{
NSMutableArray *a = value;
NSLog(@"myViewController: we have %d countries", [a count]);
NSMutableArray *countryArray = [[NSMutableArray alloc] init];
NSMutableArray *countryArrayid = [[NSMutableArray alloc] init];
for (LBCCountry *country in a ){
NSLog(@"myViewController: %d - %@", country.CountryID, country.CountryName);
[countryArray addObject:country.CountryName];
[countryArrayid addObject:[NSString stringWithFormat:@"%d",country.CountryID]];
}
self.pickerData = countryArray;
NSLog(@"pickerData: %@", pickerData);
return countryArray;
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.singlePicker = nil;
self.pickerData = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [pickerData objectAtIndex:row];
}
@end
==============
答案 0 :(得分:0)
在handleAllCountries方法中,将属性pickerData设置为countryArray,但没有强引用。因此,当该方法完成时,pickerData将为零。您应该使用:
,而不是仅将self.pickerData设置为countryArrayself.pickerData = [NSArray arrayWithArray:countryArray];
这会将country数组中的所有对象放入pickerData。