2016-07-04 12 views
-3

我有2個按鈕。如果按下第一個按鈕,我想在下一個viewController中通過index = 1,如果按下第二個按鈕,我想通過index = 2。我該怎麼做?如何在其他控制器中傳遞按鈕整數? Objective-C

代碼不起作用

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'

+0

你提的問題是過於寬泛。索引是在當前的VC還是下一個VC中聲明的?你是如何申報的?你是如何申報指數的? –

+0

也可以顯示您的按鈕IBaction。以及您試圖發送此索引值的內容。 –

回答

2

假設在你的第二個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 2個控制器,也可以創建演示文稿的代碼(在一個UINavigationController presentViewController模態Ø推)的新控制器

此外,如果你願意,你可以只創建一個IBAction爲(設置爲你是按鈕)並基於Tag屬性設置爲performSegue的sender屬性。

+0

哎呀我不看對象 - –

0

夫特

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) 
} 

目標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 
+0

更新的一部分,我的問題 – user214155

0

首先聲明在此的ViewController索引,當你點擊第一按鈕方法寫入索引= 1;相同的第二個按鈕,並在準備賽格方法做到這一點

DestinationViewController * dvc = [segue destinationViewController]; dvc.index = index;

+0

更新我的問題 – user214155

+0

更新你的問題,你在執行通過你的firstViewController –

+0

更新的問題只有一個賽格瑞 – user214155

0

我現在告訴你,我在這裏使用XIB。現在我在RootViewController中有2個按鈕。

首先我設置在視圖按鈕的按鈕標籤的XIB

然後以編程予設定的按鈕操作方法內的按鈕標籤。

- (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 
相關問題