多个UIActionSheets在同一代表 [英] Multiple UIActionSheets on the same delegate
问题描述
当我点击showErrorsActionSheet 中的按钮。该操作表从屏幕上消失,但是日志永远不会打印。
我怀疑它与向同一代表(self)声明的两个动作相关p>
- (void)checkSolution {
//代码确定BOOL的值allCorrect
if(allCorrect){//如果所有的字母都有正确的
//显示UIAlertView;
NSLog(@allCorrect);
UIActionSheet * levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@恭喜!你已经完成了!委托:self cancelButtonTitle:@查看我的工作destructiveButtonTitle:@选择下一个谜题otherButtonTitles:nil,nil];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else {
// [self showIncorrectLettersInRed];
UIActionSheet * showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@对不起,这不对,显示红色的错误?委托:self cancelButtonTitle:@不,谢谢,我会继续努力destructiveButtonTitle:@是的,我被卡住了! otherButtonTitles:nil,nil];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
应该调用的方法是: / p>
- (void)levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex!= [actionSheet cancelButtonIndex]){
NSLog(@return to levelSelect);
// pushViewController:levelSelect
}
else {
NSLog(@继续检查解决方案);
- (void)showErrorsActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex!= [ actionSheet cancelButtonIndex]){
NSLog(@show errors in red);
}
else {
NSLog(@继续尝试);
}
}
,Ive在界面文件中声明了UIActionSheet协议如下:
@interface GamePlay:UIViewController< UIActionSheetDelegate> {
为每个actionSheet设置一个标签,然后使用
分配标签
- (void)checkSolution
{
if(allCorrect)
{
UIActionSheet * levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle: @恭喜!你已经完成了!委托:self cancelButtonTitle:@查看我的工作destructiveButtonTitle:@选择下一个谜题otherButtonTitles:nil,nil];
[levelCompleteActionSheet setTag:0];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else
{
UIActionSheet * showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@对不起,这不对,显示红色错误?委托:self cancelButtonTitle:@不,谢谢,我会继续努力destructiveButtonTitle:@是的,我被卡住了! otherButtonTitles:nil,nil];
[showErrorsActionSheet setTag:1];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
UIActionSheet委托
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
开关(actionSheet.tag)
{
case 0:/ * levelCompleteActionSheet * /
{
开关(buttonIndex)
{
案例0:/ *第一个按钮* /
break ;
案例1:/ *第二按钮* /
break;
}
}
break;
案例1:/ * showErrorsActionSheet * /
break;
}
}
同样适用于此类中的其他任何地方,包括 levelCompleteActionSheet:
和 showErrorsActionSheet:
。唯一的区别是,您需要为每个actionSheet创建一个iVar,而不是在 checkSolution
中创建它们。
I'm writing a puzzle game. When the user presses the check button, I see if the solution they entered is correct. Depending on the result, I present one of two action sheets for them. For now I just have some NSLog statements to make sure things are getting called, but only one of the sheets seems to work properly.
Nothing gets called when I click a button in showErrorsActionSheet. The action sheet disappears off the screen, but the logs never print.
I suspect it has something to do with having two actionsheets declared to the same delegate (self)
- (void) checkSolution {
//code determines the value of the BOOL allCorrect
if (allCorrect) { //IF ALL OF THE LETTERS WERE CORRECT
//display UIAlertView;
NSLog(@"allCorrect");
UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:@"Review my work" destructiveButtonTitle:@"Choose next puzzle" otherButtonTitles:nil, nil];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else {
//[self showIncorrectLettersInRed];
UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:@"No Thanks, I'll keep trying" destructiveButtonTitle:@"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
the methods that are supposed to be called are:
- (void) levelCompleteActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSLog(@"return to levelSelect");
//pushViewController:levelSelect
}
else {
NSLog(@"continue to examine solution");
}
}
- (void) showErrorsActionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSLog(@"show errors in red");
}
else {
NSLog(@"continue to try");
}
}
and Ive declared the UIActionSheet protocol in the interface file as follows:
@interface GamePlay : UIViewController <UIActionSheetDelegate> {
Set a tag for each actionSheet, then use a switch
statement in the UIActionSheet delegate.
Assign a tag
- (void)checkSolution
{
if (allCorrect)
{
UIActionSheet *levelCompleteActionSheet = [[UIActionSheet alloc] initWithTitle:@"Congratulations! You Have Finished the Level!" delegate:self cancelButtonTitle:@"Review my work" destructiveButtonTitle:@"Choose next puzzle" otherButtonTitles:nil, nil];
[levelCompleteActionSheet setTag: 0];
[levelCompleteActionSheet showInView:self.view];
[levelCompleteActionSheet release];
}
else
{
UIActionSheet *showErrorsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Sorry, thats not right. Show errors in red?" delegate:self cancelButtonTitle:@"No Thanks, I'll keep trying" destructiveButtonTitle:@"Yes please, I'm stuck!" otherButtonTitles:nil, nil];
[showErrorsActionSheet setTag: 1];
[showErrorsActionSheet showInView:self.view];
[showErrorsActionSheet release];
}
}
UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch ( actionSheet.tag )
{
case 0: /* levelCompleteActionSheet */
{
switch ( buttonIndex )
{
case 0: /* 1st button*/
break;
case 1: /* 2nd button */
break;
}
}
break;
case 1: /* showErrorsActionSheet */
break;
}
}
The same would apply anywhere else in this class as well, including levelCompleteActionSheet:
and showErrorsActionSheet:
. The only difference is, you would need to create an iVar for each actionSheet instead of creating them in checkSolution
.
这篇关于多个UIActionSheets在同一代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!