0

我从数据类型的 web 服务中获得了成功的结果NSMutableArray,但我的困境是如何将内容结果显示给我的UIPickerView对象?当我运行这个时,没有出现任何内容UIPickerView,但是当我点击按钮时,它从网络服务内容中弹出了第一个国家。任何评论将不胜感激,这里有我的代码:

我的视图控制器.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

===============

4

1 回答 1

0

在 handleAllCountries 方法中,您将属性 pickerData 设置为没有强引用的 countryArray。因此,当该方法完成时,pickerData 将为 nil。而不是仅仅将 self.pickerData 设置为 countryArray,您应该使用:

self.pickerData = [NSArray arrayWithArray:countryArray];

这会将国家数组中的所有对象放入pickerData。

于 2012-09-09T00:07:05.180 回答