-1

On a new project, I have 2 view controllers (View1 and View2).
I have a button in View1 and a label in View2. When I press this button in View1, I want the name/title of the button to appear in the label on View2 when I am transferred to the View2 page. How can I do this?

I checked this page but didn't really understand: Passing button title to a new view

Community
  • 1
  • 1
tabbi
  • 1
  • 3
  • what didn't you understand about it? – Wain Jan 07 '16 at 12:56
  • I got errors when I typed them in. – tabbi Jan 07 '16 at 13:00
  • that's because they're in obj-c, not swift - but do you understand the principle that the answer is trying to explain about how you should approach the problem? – Wain Jan 07 '16 at 13:02
  • I don't quite understand the "you need to create a property" bit – tabbi Jan 07 '16 at 13:05
  • that exposes the label on the second view so you can pass it information – Wain Jan 07 '16 at 13:10
  • See: http://stackoverflow.com/a/31934786/2535467 – CaptJak Jan 07 '16 at 17:40
  • 1
    Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – CaptJak Jan 07 '16 at 17:40
  • Are you using storyboard? There are 2 ways to handle this.....I would like to know if the button you will tap will perform a segue ??? – OverD Jan 07 '16 at 21:31
  • @CaptJak As you said, it is a duplicate. Just in a different language. I don't have enough experience with programming to understand it in Objective C and convert it to Swift. – tabbi Jan 08 '16 at 07:56
  • See: stackoverflow.com/a/31934786/2535467. It's in swift. – CaptJak Jan 08 '16 at 13:56

2 Answers2

2

Create a new CocoaTouch Class SecondViewController, subclassing UIViewController including XIB and create IBOutlet for the label

Declare a variable:

var labelTitle: String! in SecondViewController

SecondViewController ViewDidLoad will look like this:

override func viewDidLoad() {
            super.viewDidLoad()

            titleLabel.text = labelTitle
}

// Whereas titleLabel is the UILabel Outlet connected from XIB

Now in your FirstViewController, on your button Tapped action

add the following

@IBAction func buttonAction(sender : UIButton) {
    // If the VCs are in a storyboard you will need to get the storyboard to access them
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) as UIStoryboard
    let secondVC = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
    secondVC.labelTitle = sender.titleLabel?.text

    // If not do this
    let secondVC = SecondViewController.init(nibName: "SecondViewController", bundle: nil)
    secondVC.labelTitle = sender.titleLabel?.text
}

//Next present or push your ViewController
brl214
  • 527
  • 1
  • 6
  • 16
Mohammed Shakeer
  • 1,446
  • 16
  • 23
-1

- On View2 create property for accepting button title

@interface View2 : UIViewController {

}
@property (nonatomic, strong) IBOutlet UILabel *lblTitle;
@property (nonatomic, strong) NSString *btnTitle;

- (void)viewDidLoad {
    [super viewDidLoad];
   self.lblTitle.text = self.btnTitle;
}

In View1

  @interface View1 : UIViewController {

    }
    @property (nonatomic, strong) IBOutlet UIButton *btn;

    - (void)myButtonClicked:(id)sender{

    View1 *newViewController = [[View1 alloc] initWithNibName:'View1' bundle:nil];
    newViewController.btnTitle = self.btn.titleLabel.text;

    // push or present controller

    }
darshan
  • 1,115
  • 1
  • 14
  • 29