Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从UIAlertView按钮展开分段_Ios_Objective C_Xcode_Uistoryboardsegue - Fatal编程技术网

Ios 如何从UIAlertView按钮展开分段

Ios 如何从UIAlertView按钮展开分段,ios,objective-c,xcode,uistoryboardsegue,Ios,Objective C,Xcode,Uistoryboardsegue,我有3个视图控制器。第一个是从我的根视图控制器以模态方式呈现的 它们位于导航堆栈中,使用从第一个到第三个的push 我的最终视图控制器(第三个)有一个UIAlertView,按下ok后,我想将其展开到第一个视图控制器 然而,我所读到的任何关于这种展开的信息都表明,它是由系统通过故事板按钮激活的 如何从第三视图控制器放回到第一视图控制器?我曾经 [self performSegueWithIdentifier:segueString sender:self]; 但它会在再次尝试导航时使应用程序崩

我有3个视图控制器。第一个是从我的根视图控制器以模态方式呈现的

它们位于导航堆栈中,使用从第一个到第三个的
push

我的最终视图控制器(第三个)有一个
UIAlertView
,按下ok后,我想将其展开到第一个视图控制器

然而,我所读到的任何关于这种展开的信息都表明,它是由系统通过故事板按钮激活的

如何从第三视图控制器放回到第一视图控制器?我曾经

[self performSegueWithIdentifier:segueString sender:self];
但它会在再次尝试导航时使应用程序崩溃

[self.navigationController popToRootViewController] 

将帮助您解决问题,因此您需要使用UIAlertView委托方法,如下所示

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  //Using buttonIndex you could find whether OK or CANCEL is pressed and perform your popToRootViewController call.
  //Below on OK press then moving to root VC
  if (buttonIndex==0)
  {
     //OK is pressed...
    [self.navigationController popToRootViewControllerAnimated:YES];
  }
  else{ 
     //CANCEL is pressed...
  }
}

希望这对您有所帮助。

由@walle84给出的解决方案非常好。。。但这并不是一个放松阶段的解决方案。如果您想使用真正的展开段,请按照此步骤逐步操作

在FirstViewController.m文件中,添加一个
-reset:
方法,使代码如下所示:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

-(IBAction)reset:(UIStoryboardSegue *)segue {
    //you can do stuff here, if necessary
    //popToRootViewControllerAnimated doesn't allow it
    NSLog(@"Back to FirstViewController");
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
#import "ThirdViewController.h"

@interface ThirdViewController () <UIAlertViewDelegate>

@end

@implementation ThirdViewController

- (IBAction)buttonPressed:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"MyApp"
                                                      message:@"Do you want to reset to FirstViewController?"
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:nil];

    [message addButtonWithTitle:@"Perform Unwind"];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [self performSegueWithIdentifier:@"myUnwindSegue" sender:self];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
在ThirdViewController.m中,设置
-alertView:ClickedButtonIndex:
方法,使代码如下所示:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

-(IBAction)reset:(UIStoryboardSegue *)segue {
    //you can do stuff here, if necessary
    //popToRootViewControllerAnimated doesn't allow it
    NSLog(@"Back to FirstViewController");
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
#import "ThirdViewController.h"

@interface ThirdViewController () <UIAlertViewDelegate>

@end

@implementation ThirdViewController

- (IBAction)buttonPressed:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"MyApp"
                                                      message:@"Do you want to reset to FirstViewController?"
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:nil];

    [message addButtonWithTitle:@"Perform Unwind"];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [self performSegueWithIdentifier:@"myUnwindSegue" sender:self];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
#导入“ThirdViewController.h”
@接口ThirdViewController()
@结束
@第三代虚拟控制器的实现
-(iAction)按钮按下:(id)发件人{
UIAlertView*消息=[[UIAlertView alloc]initWithTitle:@“MyApp”
消息:@“是否要重置为FirstViewController?”
代表:赛尔夫
取消按钮:@“取消”
其他按钮:无];
[标题为@“执行展开”的消息添加按钮];
[信息显示];
}
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引{
如果(按钮索引==1){
[self-PerformsgueWithIdentifier:@“myUnwindSegue”发送方:self];
}
}
-(无效)viewDidLoad{
[超级视图下载];
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
@结束
然后,在情节提要中,选择
ThirdViewController
场景,单击
Third View Controller
按钮并将其拖动到
退出
按钮(参见下图)。将出现一个弹出窗口。选择其中的
Reset:
链接

完成后,选择
unwindsegue
按钮(见下图),并在属性检查器中将其名称设置为“myUnwindSegue”(见下图)


如果您想了解更多关于展开序列的信息,您可以观看WWDC 2012会话视频“在应用程序中采用故事板”[开始时间为38分钟]。

使用[self.navigationcontroller poptorootviewcontroller]方法