从导航栏中的按钮向视图控制器创建show segue

时间:2021-10-07 17:27:21

i have a button in nav bar , when user click it , we move to the next view controller page. It has some validations in form when all the validation are fulfill than we move to next page.I'm confused that how can i programatically create a show segue on this button in nav bar.I want to create it because i want to take the value from this page towards the second one through segue.How can i do this?

我在导航栏中有一个按钮,当用户单击它时,我们移动到下一个视图控制器页面。当所有验证都完成时,它有一些形式的验证,而不是我们移动到下一页。我很困惑,我怎么能以编程方式在导航栏中的这个按钮上创建一个show segue。我想创建它因为我想要从这个页面到第二个到segue的价值。我怎么能这样做?

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                initWithTitle:@""
                                style:UIBarButtonItemStyleDone
                                target:self
                                action:@selector(rightBtnClicked)];
[rightButton setImage:[UIImage imageNamed:@"right.png"]];
rightButton.tintColor=[UIColor whiteColor];

self.navigationItem.rightBarButtonItem = rightButton;

Here is rightBtnClicked method

这是rightBtnClicked方法

-(void)rightBtnClicked:(id)sender{
[self Validation];
//[self performSegueWithIdentifier:@"Submit" sender:sender];
NSLog(@"Am here");
SubmitImageViewController *secondVC = [[SubmitImageViewController alloc] initWithNibName:@"Images" bundle:nil];
secondVC.tites=Title.text;
secondVC.propfor=_but1.titleLabel.text;
secondVC.proptype=_but2.titleLabel.text; //secondVC's variable to which u wanna pass value to
[self.navigationController pushViewController:secondVC animated:true];

}

}

2 个解决方案

#1


1  

Show segue is nothing more than pushing a new VC to your VC's navigation stack.

显示segue只不过是将新VC推送到VC​​的导航堆栈。

If you are not using storyboard and want to do it programmatically you can do

如果您不使用故事板并希望以编程方式执行,则可以执行此操作

let secondVC = CViewController(nibName: "your_nib_name", bundle: nil)
secondVC.params = value
self.navigationController?.pushViewController(secondVC, animated: true)

You don't need prepareForSegue to get the access to second VC because u already have a VC which you are pushing :)

你不需要prepareForSegue来访问第二个VC,因为你已经拥有了一个VC,你正在推动它:)

EDIT:

编辑:

As OP asked for Objective-C code updating my answer

OP要求Objective-C代码更新我的答案

SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondVC.params = value; //secondVC's variable to which u wanna pass value to
[self.navigationController pushViewController:secondVC animated:true];

EDIT #2:

编辑#2:

As OP is facing crash am updating my answer to show how to write selector to UIBarButtonItem action

由于OP面临崩溃我更新了我的答案,以展示如何将选择器写入UIBarButtonItem动作

You should write the method rightButtonClicked in the same class where you are adding the bar button to Navigation bar.

您应该在将条形按钮添加到导航栏的同一个类中编写方法rightButtonClicked。

For example : If you are adding button in ViewController.m then write

例如:如果要在ViewController.m中添加按钮,则写入

-(void)rightBtnClicked {
    NSLog(@"Am here");
    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondVC.params = value; //secondVC's variable to which u wanna pass value to
    [self.navigationController pushViewController:secondVC animated:true];
}

EDIT #3:

编辑#3:

Fixing the bug in OP's code

修复OP代码中的错误

change

更改

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                initWithTitle:@""
                                style:UIBarButtonItemStyleDone
                                target:self
                                action:@selector(rightBtnClicked)];

to

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                initWithTitle:@""
                                style:UIBarButtonItemStyleDone
                                target:self
                                action:@selector(rightBtnClicked:)];

Look the way we are calling rightBtnClicked with colon.

看看我们用冒号调用rightBtnClicked的方式。

EDIT #4:

编辑#4:

As OP's isn't aware of how to instantiate VC from storyboard updating my answer

因为OP不知道如何从故事板中实例化VC更新我的答案

Step1 : Set the storyboard Identifier for your SubmitImageViewController Open your storyboard, select the SubmitImageViewController and set the class and storyboard id as shown below.

步骤1:为SubmitImageViewController设置storyboard标识符打开故事板,选择SubmitImageViewController并设置类和故事板ID,如下所示。

从导航栏中的按钮向视图控制器创建show segue

Look carefully the Storyboard id "submitImageVC" string.

仔细查看Storyboard id“submitImageVC”字符串。

Step 2:

第2步:

Now instantiate VC from storyboard using this storyboard id

现在使用此storyboard id从storyboard实例化VC

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *secondVC = [storyBoard instantiateViewControllerWithIdentifier:@"submitImageVC"];
[self.navigationController pushViewController:secondVC animated:true];

Please specify the proper storyboard name and storyboard id and u'll be fine to go :)

请指定正确的故事板名称和故事板ID,你可以去:)

#2


0  

In Selector method :

在选择器方法中:

-(IBAction)rightBtnClicked:(id)sender{
        [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:sender];
    }

Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Implement -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

只需在prepareForSegue:方法中获取对目标视图控制器的引用,并将所需的任何对象传递给那里。实现 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if ([segue.identifier isEqualToString:@"yourSegueIdentifier"]){
            SecondViewController * secondVC = segue.destinationViewController;
            secondVC.params = value; 
        }
    }

#1


1  

Show segue is nothing more than pushing a new VC to your VC's navigation stack.

显示segue只不过是将新VC推送到VC​​的导航堆栈。

If you are not using storyboard and want to do it programmatically you can do

如果您不使用故事板并希望以编程方式执行,则可以执行此操作

let secondVC = CViewController(nibName: "your_nib_name", bundle: nil)
secondVC.params = value
self.navigationController?.pushViewController(secondVC, animated: true)

You don't need prepareForSegue to get the access to second VC because u already have a VC which you are pushing :)

你不需要prepareForSegue来访问第二个VC,因为你已经拥有了一个VC,你正在推动它:)

EDIT:

编辑:

As OP asked for Objective-C code updating my answer

OP要求Objective-C代码更新我的答案

SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondVC.params = value; //secondVC's variable to which u wanna pass value to
[self.navigationController pushViewController:secondVC animated:true];

EDIT #2:

编辑#2:

As OP is facing crash am updating my answer to show how to write selector to UIBarButtonItem action

由于OP面临崩溃我更新了我的答案,以展示如何将选择器写入UIBarButtonItem动作

You should write the method rightButtonClicked in the same class where you are adding the bar button to Navigation bar.

您应该在将条形按钮添加到导航栏的同一个类中编写方法rightButtonClicked。

For example : If you are adding button in ViewController.m then write

例如:如果要在ViewController.m中添加按钮,则写入

-(void)rightBtnClicked {
    NSLog(@"Am here");
    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondVC.params = value; //secondVC's variable to which u wanna pass value to
    [self.navigationController pushViewController:secondVC animated:true];
}

EDIT #3:

编辑#3:

Fixing the bug in OP's code

修复OP代码中的错误

change

更改

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                initWithTitle:@""
                                style:UIBarButtonItemStyleDone
                                target:self
                                action:@selector(rightBtnClicked)];

to

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                initWithTitle:@""
                                style:UIBarButtonItemStyleDone
                                target:self
                                action:@selector(rightBtnClicked:)];

Look the way we are calling rightBtnClicked with colon.

看看我们用冒号调用rightBtnClicked的方式。

EDIT #4:

编辑#4:

As OP's isn't aware of how to instantiate VC from storyboard updating my answer

因为OP不知道如何从故事板中实例化VC更新我的答案

Step1 : Set the storyboard Identifier for your SubmitImageViewController Open your storyboard, select the SubmitImageViewController and set the class and storyboard id as shown below.

步骤1:为SubmitImageViewController设置storyboard标识符打开故事板,选择SubmitImageViewController并设置类和故事板ID,如下所示。

从导航栏中的按钮向视图控制器创建show segue

Look carefully the Storyboard id "submitImageVC" string.

仔细查看Storyboard id“submitImageVC”字符串。

Step 2:

第2步:

Now instantiate VC from storyboard using this storyboard id

现在使用此storyboard id从storyboard实例化VC

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *secondVC = [storyBoard instantiateViewControllerWithIdentifier:@"submitImageVC"];
[self.navigationController pushViewController:secondVC animated:true];

Please specify the proper storyboard name and storyboard id and u'll be fine to go :)

请指定正确的故事板名称和故事板ID,你可以去:)

#2


0  

In Selector method :

在选择器方法中:

-(IBAction)rightBtnClicked:(id)sender{
        [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:sender];
    }

Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Implement -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

只需在prepareForSegue:方法中获取对目标视图控制器的引用,并将所需的任何对象传递给那里。实现 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if ([segue.identifier isEqualToString:@"yourSegueIdentifier"]){
            SecondViewController * secondVC = segue.destinationViewController;
            secondVC.params = value; 
        }
    }