从导航栏中的按钮向视图控制器

时间:2017-07-14 06:14:15

标签: ios objective-c

我在导航栏中有一个按钮,当用户点击它时,我们移动到下一个视图控制器页面。当所有验证都完成时,它有一些形式的验证,而不是我们移动到下一页。我很困惑,我怎么能以编程方式在导航栏中的这个按钮上创建一个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;

这是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 个答案:

答案 0 :(得分:1)

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

如果您不使用故事板并想以编程方式执行此操作

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

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

修改

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];

编辑#2:

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

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

例如:如果要在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];
}

编辑#3:

修复OP代码中的错误

更改

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

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

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

编辑#4:

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

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

enter image description here

仔细查看Storyboard id" submitImageVC"字符串。

第2步:

现在使用此故事板id

从故事板中实例化VC
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *secondVC = [storyBoard instantiateViewControllerWithIdentifier:@"submitImageVC"];
[self.navigationController pushViewController:secondVC animated:true];

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

答案 1 :(得分:0)

在选择器方法中:

-(IBAction)rightBtnClicked:(id)sender{
        [self performSegueWithIdentifier:@"yourSegueIdentifier" sender: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; 
        }
    }