【问题标题】:How do you detect what UIPickerView row has been selected using an if statement你如何检测使用 if 语句选择了 UIPickerView 行
【发布时间】:2012-07-03 08:12:21
【问题描述】:
我制作了一个按钮,以便当用户按下它并选择特定行时,它会执行某些操作。
到目前为止,我有这个:
if (pickerView selectedRowInComponent:0) {
[mailComposerTwo setToRecipients:[NSArray arrayWithObjects:@"email@blah.com",nil]];
}
它自己工作。但是当我多次执行 if 语句时,它会崩溃。
让它发挥作用的方法?
感谢您的帮助。
【问题讨论】:
标签:
if-statement
xcode4.2
uipickerview
【解决方案1】:
问题可能在于您的邮件编辑器,而不是选择器视图。显示作曲家时,请确保仅在尚未创建时才创建它。
另外,请确保在展示后释放它:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
...[configure the picker]
[rootContainer presentModalViewController:picker animated:YES];
[picker release];
【解决方案2】:
NSArray *finalList = [[NSArray alloc]init];
//put all your if statements
if (pickerView selectedRowInComponent:0)
{
[finalList arrayByAddingObjectsFromArray:@[@"email@address.com",@"second@address.com",...];
}
if (pickerView selectedRowInComponent:1)
{
[finalList arrayByAddingObjectsFromArray:@[@"another@address.com",@"fourth@address.com",...];
}
//end of if statements
[mailComposerTwo setToRecipients:finalList];
[self presentViewController:yourInitializedMessageController animated:YES completion:^{NSLog(@"message controller is presented");}];
这将执行单个方法调用,而不是不断地重新分配由于某些奇怪原因导致您的异常的方法。 presentModalViewController:animated: 自 iOS 6.0 起已被弃用?如果不是 7.0 我相信。
注意!使消息控制器成为主视图控制器的属性。这是一种很好的做法,因此如果您需要恢复它,iOS 不会自动发布它。但是,如果您使用 MFMessageComposer,iOS 将保持 messenger 分配或在某个线程中运行,因此为它初始化视图控制器很快。