我有一个if
声明工作正常,但我需要添加第二个if
其中的声明,我似乎不知道如何正确执行。
这是我的代码:
-(IBAction)xButton {
if([_hasUserTakenAPhoto isEqual: @"YES"]) {
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:@"Delete" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
NSString *title = [_xButtonAfterPhotoTaken buttonTitleAtIndex:1];
if(title isEqualToString:@"Delete") {
[self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}
} else {
[self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}
}
当我添加第二个 if 语句时出现错误:
if(title isEqualToString:@"Delete") {
[self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}
我尝试将第二个 if 语句设为“else if”,但它不会让我访问名为“title”的 NSString 对象。有没有更简单的方法来做到这一点,或者我应该将 title 设置为全局变量?
UIActionSheet
s 不以这种方式使用:
- (IBAction)xButton:(UIButton*)sender
{
if ([_hasUserTakenAPhoto isEqual:@"YES"])
{
_xButtonAfterPhotoTaken = [[UIActionSheet alloc] initWithTitle:@"Delete Photo?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
[_xButtonAfterPhotoTaken showInView:self.view];
}
else
{
[self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Check if it's the correct action sheet and the delete button (the only one) has been selected.
if (actionSheet == _xButtonAfterPhotoTaken && buttonIndex == 0)
{
[self performSegueWithIdentifier:@"backToHomeFromMediaCaptureVC" sender:self];
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(@"Canceled");
}
您必须了解界面元素不是“即时”的,存在大量异步情况。当呈现一个UIActionSheet
例如,线程不会等待用户回答是或否,它会继续运行。
这就是为什么有代表和区块,你提出UIActionSheet
,并且对委托说“当用户实际单击它时我会处理它”。
您可能想知道,为什么不等待它选择它呢?主线程负责更新界面、动画并检索用户输入(触摸、键盘点击等),甚至运行NSTimers
是主函数的下标NSRunLoop
。停止主线程会锁定界面。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)