好的,我会试着解释一下:
你声明了一个NSMutbleArray,你不能指望使用NSArray,因为它是不可变的,你需要修改内容。
NSMutableArray *array;
UIAlertController * view;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray* friends = [NSArray arrayWithObjects: @"jim", @"joe", @"anne", nil];
array = [NSMutableArray arrayWithArray:friends];
// I added a UIPickerView in the StoryBoard, connected it to a property and the delegates and datasources.
self.pickerView.delegate = self;
}
然后你声明UIPickerView的dataSource:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return array[row];
}
那么,然后你展示你的 UIAlertController,在这种情况下,我将在 TextView 的 Return 中将其关闭。
//This is an action connected to the button which will present the ActionController
- (IBAction)addFriend:(id)sender {
view = [UIAlertController alertControllerWithTitle:@"Add Friend" message:@"Write the friend to add"preferredStyle:UIAlertControllerStyleAlert];
[view addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Friend";
textField.delegate = self;
textField.tag = 01;
}];
[self presentViewController:view animated:YES completion:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if(textField.tag == 01){
[array insertObject:textField.text atIndex:array.count];
[view dismissViewControllerAnimated:YES completion:^{
[self.pickerView reloadAllComponents];
}];
return YES;
}
return YES;
}
这或多或少是你能做的,我是最具体的,因为你说你是一个初学者。
希望对你有帮助。