如何通过单击 iPhone 故事板中的 ActionSheet 按钮移动到下一个 viewController

Posted

技术标签:

【中文标题】如何通过单击 iPhone 故事板中的 ActionSheet 按钮移动到下一个 viewController【英文标题】:How can I Move to next viewController by clicking ActionSheet Buttons in Storyboard in iphone 【发布时间】:2013-02-15 05:13:42 【问题描述】:

我正在开发一个应用程序,因为我需要一个要求,即当我单击 ActionSheet 按钮时,我想转到 nextviewcontroller。我尝试使用休闲代码,但它不会转到下一个 viewcontroller。我如何在 storybord.can 中连接 segues任何人都可以帮助我。谢谢提前...

#import <UIKit/UIKit.h>
#import "SecondViewController.h"



@interface Tab2ViewController : UIViewController<UIActionSheetDelegate>


    UIActionSheet *actionSheet;
    SecondViewController *secondViewController;

@end



#import "Tab2ViewController.h"

@interface Tab2ViewController ()

@end

@implementation Tab2ViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib. 


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(60.0f, 180.0f, 200.0f,70.0f);
    [button setTitle:@"Show Action Sheet" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tintColor = [UIColor darkGrayColor];
    [button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];





- (void)showActionSheet:(id)sender

     NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title
     NSString *destructiveTitle = @"Destructive Button"; //Action Sheet Button Titles
     NSString *other1 = @"Other Button 1";
     NSString *other2 = @"Other Button 2";
     NSString *other3 = @"Other Button 3";
     NSString *cancelTitle = @"Cancel Button";
     UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:actionSheetTitle delegate:self
     cancelButtonTitle:cancelTitle destructiveButtonTitle:destructiveTitle
     otherButtonTitles:other1, other2, other3, nil];
      actionSheet1.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
     [actionSheet1 showInView:self.view];




-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    NSLog(@"prepareForSegue1");
    if([[segue identifier] isEqualToString:@"third"])
    
         NSLog(@"prepareForSegue2");
        secondViewController=[segue destinationViewController];       

    


- (void)actionSheet:(UIActionSheet *)actionSheet1 clickedButtonAtIndex:(NSInteger)buttonIndex

    //Get the name of the current pressed button
    NSString *buttonTitle = [actionSheet1 buttonTitleAtIndex:buttonIndex];
    if  ([buttonTitle isEqualToString:@"Destructive Button"])
    
        NSLog(@"Destructive pressed --> Delete Something");
        [self performSegueWithIdentifier:@"third" sender:nil];
    
    if ([buttonTitle isEqualToString:@"Other Button 1"])
    
        NSLog(@"Other 1 pressed");
        [self performSegueWithIdentifier:@"third" sender:nil];
    
    if ([buttonTitle isEqualToString:@"Other Button 2"])
    
        NSLog(@"Other 2 pressed");
        [self performSegueWithIdentifier:@"third" sender:nil];
    
    if ([buttonTitle isEqualToString:@"Other Button 3"])
    
        NSLog(@"Other 3 pressed");
        [self performSegueWithIdentifier:@"third" sender:nil];
    
    if ([buttonTitle isEqualToString:@"Cancel Button"])
    
        NSLog(@"Cancel pressed --> Cancel ActionSheet");
        [self performSegueWithIdentifier:@"third" sender:nil];
    


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.



@end

【问题讨论】:

【参考方案1】:

如果您想打开新的 Viewcontroller,请尝试以下行,希望对您有所帮助:

   UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
   Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Synergy"];
  [self presentModalViewController:objSynergy animated:YES];

Synergy 是我的新控制器名称,您将其替换为您的类名称根据您的要求更改此代码。

【讨论】:

【参考方案2】:

试试这个。它对我有用::

if ([buttonTitle isEqualToString:@"Other Button 1"]) 

    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    Paypal_ViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"Paypal_ViewController"];
    [self presentViewController:vc animated:YES completion:nil];


【讨论】:

以上是关于如何通过单击 iPhone 故事板中的 ActionSheet 按钮移动到下一个 viewController的主要内容,如果未能解决你的问题,请参考以下文章

如何使故事板中的 UiView 尊重百分比[重复]

故事板中iPhone6 Plus的布局视图

如何更新动态生成的故事板中的自定义特定 UITableview 单元格?

IOS开发。如何通过 segue 连接不同故事板中的两个场景

如何保存 AVPlayer 中的最后暂停时间,并在另一个故事板中寻找它?

在通用故事板中使用 present 作为弹出框