我有两个按钮。如果按下第一个按钮,我想在下一个viewController
中传递index = 1
,如果按下第二个按钮,则传递index = 2
。我该怎么做呢?
代码不工作
FirstController.h
@property (nonatomic, assign) NSInteger index;
FirstController.m
-(IBAction)Method1:(id)sender
{
self.index = 1;
[self performSegueWithIdentifier:@"Segue" sender:self];
}
-(IBAction)Method2:(id)sender
{
self.index = 2;
[self performSegueWithIdentifier:@"Segue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ nextViewController *dvc=[segue destinationViewController];
dvc.index= _index;
}
next.h
@property (nonatomic, assign) NSInteger index;
next.m
if (self.index == 2){
_data1 = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],nil];
}
Terminating app due to uncaught exception ‘NSInvalidArgumentException', reason: '-[RootViewController setIndex:]: unrecognized selector sent to instance 0x7faff1f18c30'
假设在你的第二个UIViewController中你有这样的东西:
class SecondViewController:UIViewController{
var index:Int!
}
然后在你的第一个UIViewController中你会有这样的东西:
class FirstViewController : UIViewController{
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue_first_second"{
if let index = sender as? Int{
let secondViewController = segue.destinationViewController as! SecondViewController
secondViewController.index = index
}
}
}
@IBAction func firstButtonTapped(sender:UIButton){
let index = 1
self.performSegueWithIdentifier("segue_first_second", sender: index)
}
@IBAction func firstButtonTapped(sender:UIButton){
let index = 2
self.performSegueWithIdentifier("segue_first_second", sender: index)
}
}
注意:您需要将IBAction链接到您的按钮。还需要在你的两个控制器之间建立一个segue或者你可以通过代码创建一个新的控制器(presentViewController模式为UINavigationController)
如果你愿意,你可以只创建一个IBAction(设置为你的按钮),并基于Tag属性,设置为performSegue的sender属性
我现在告诉你。这里我用XIB。现在我有两个按钮在RootViewController。
首先,我在XIB
中设置了button标签然后通过编程的方式在button action方法中设置button标签。
- (IBAction)actionGoFirstViewController:(id)sender
{
// This is for getting button tag from the button view of XIB
UIButton *btnFirst = sender;
//OR else I can set the button tag here.
btnFirst.tag = 1;
FirstViewController *firstVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
firstVC.intFirstBtntag = btnFirst.tag;
//OR directly We can set the tag.
firstVC.intFirstBtntag = 1;
[self.navigationController pushViewController:firstVC animated:YES];
}
- (IBAction)actionGoSecondViewController:(id)sender
{
// This is for getting button tag from the button view of XIB
UIButton *btnSecond = sender;
//OR else I can set the button tag here.
btnSecond.tag = 2;
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
secondVC.intSecondButtonTag = btnSecond.tag;
//OR directly We can set the tag.
secondVC.intSecondButtonTag = 2;
[self.navigationController pushViewController:secondVC animated:YES];
}
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *lblFirstButtonIndex;
@property (nonatomic, assign) NSInteger intFirstBtntag;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize lblFirstButtonIndex;
@synthesize intFirstBtntag;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
lblFirstButtonIndex.text = [NSString stringWithFormat:@"The First Button Tag is - %ld",(long)intFirstBtntag];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionBackRootView:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *lblSecondButtonIndex;
@property (nonatomic, assign) NSInteger intSecondButtonTag;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize lblSecondButtonIndex;
@synthesize intSecondButtonTag;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
lblSecondButtonIndex.text = [NSString stringWithFormat:@"The Second button Tag is - %ld",(long)intSecondButtonTag];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionBackToRoot:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
Swift
var index = 0
@IBAction func button1(sender: UIButton) {
index = 1
}
@IBAction func button2(sender: UIButton) {
index = 2
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifier" {
if let nextVC = segue.destinationViewController as? NextViewController {
nextVC.index = index
}
}
}
//If not storyBoard
func pushToNext() {
let nextVC = NextViewController()
nextVC.index = index
navigationController?.pushViewController(nextVC, animated: true)
}
objective - c #import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()
@property (nonatomic, assign) NSInteger index;
@property (nonatomic) UIButton *button1;
@property (nonatomic) UIButton *button2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.index = 0;
self.button1 = [[UIButton alloc]initWithFrame:YourFrame1];
[self.button1 addTarget:self action:@selector(clickButton1) forControlEvents:UIControlEventTouchUpInside];
self.button2 = [[UIButton alloc]initWithFrame:YourFrame2];
[self.button1 addTarget:self action:@selector(clickButton2) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)clickButton1 {
self.index = 1;
}
- (void)clickButton2 {
self.index = 2;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Your identifier"]) {
NextViewController *nextVC = segue.destinationViewController;
nextVC.index = self.index;
}
}
- (void)pushToNextVC {
//Storyboard
NextViewController *nextVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"Your storyboard identifier"];
//Not storyboard
NextViewController *nextVC = [[NextViewController alloc]init];
nextVC.index = self.index;
[self.navigationController pushViewController:nextVC animated:YES];
}
@end
首先声明索引在这个ViewController和当你点击第一个按钮方法写索引=1;第二个按钮也是一样在prepareforsegue方法中做这个
DestinationViewController *dvc=[segue DestinationViewController];dvc.index =指数;