2016-07-04 6 views
-3

Ich habe 2 Tasten. Ich möchte index = 1 in nächsten viewController übergeben, wenn die erste Taste gedrückt wurde und index = 2, wenn die zweite Taste gedrückt wurde. Wie kann ich es tun?Wie Taste Ganzzahl in anderen Controller übergeben? Objective-C

-Code funktioniert nicht

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

Ihre Frage ist zu weit gefasst. Wird der Index sowohl in der aktuellen VC als auch in der nächsten VC deklariert? Wie hast du sie erklärt? Wie haben Sie den Index deklariert? –

+0

auch pls zeigen Sie Ihre Taste IBaction. und was Sie versucht haben, diesen Indexwert zu senden. –

Antwort

2

Angenommen, in Ihrem zweiten UIViewController haben Sie so etwas wie:

class SecondViewController:UIViewController{ 
    var index:Int! 
} 

Dann in Ihrem ersten UIViewController Ihr geht etwas zu haben, wie:

Hinweis: Sie müssen die IBAction mit Ihren Tasten verknüpfen. müssen auch ein Übergang zwischen zwei Controller aufzubauen oder Sie können die Präsentation der neuen Controller von Code (presentViewController modal o Push auf einem UINavigationController)

auch erstellen, wenn Sie möchten, können Sie nur eine IBAction (Set erstellen Sie sind Schaltflächen) und basieren auf der Eigenschaft Tag, die auf die Absendereigenschaft von performSegue festgelegt ist.

+0

Upps ich nicht den Teil des Obj-C –

0

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

aktualisieren meine Frage lesen – user214155

0

ersten declare Index in diesem Viewcontroller und bei der ersten Schaltfläche Methode Schreibindex klicken = 1; Selben für die zweite Taste und die in Vorbereitung für die Übergabemethode tun Sie dies

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

+0

meine Frage Aktualisieren – user214155

+0

aktualisiert Ihre Frage sind Sie nur ein Segue durch Ihre firstViewController –

+0

Update Frage Durchführung – user214155

0

Ich sage dir jetzt.Ich benutze XIB hier.Jetzt habe ich 2 Tasten in RootViewController.

Zuerst habe ich die Ohrenmarke in Anbetracht Taste in der XIB

Dann programmatisch ich die Ohrenmarke innerhalb der Schaltfläche Aktionsmethode eingestellt.

- (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