Access to user interface elements created in Interface Builder in the controller?

This is a very simple iPhone / Cocoa question. I have a button that jumps between two views. I installed most of this using an interface constructor. When I press the button and the second view is displayed, how do I programmatically change the button text (for example, "back")?

+3
source share
5 answers

In the class declaration, declare the button object and make sure it is listed as an IBOutlet:

IBOutlet UIButton* myButton;

, Builder, , ( , ). UIButton .

.

UIButton, :

[myButton setTitle:@"Back - or whatever else you want it to say" 
          forState: UIControlStateNormal ];
// you can set different title text for each state 
// of the button (selected, active, or normal)
+10

-[UIView viewWithTag:], UIView . , , .

+4

IB:

@interface MyViewController : UIViewController {
    IBOutlet UIButton *myButton;
}

, IB, MyViewController, UIView UIButton .

:

[myButton setTitle:@"Back" forState:UIControlStateNormal];

, - http://www.stanford.edu/class/cs193p/cgi-bin/index.php, iPhone.

, .

+2

. :

, , (, "" )?

, , - UINavigationBarController plug-n-play. :

// In firstViewController.m
self.navigationController = [[UIViewController alloc]initWithNibName:@"secondView" bundle:nil];
[self pushViewController:secondViewController animated:TRUE];

, . , :

// In secondViewController.m
-(void)ViewWillAppear
{
  [self.navigationItem.backBarButtonItem setText:@"GoBack"];
}

BarButtonItem (), . n-play:

// In secondViewController.h
-(IBAction)goBack; (this should appear as an action in your associated nib file)

// In secondViewController.m
-(void)viewDidLoad
{
  self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"GoBack" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
}

-(IBAction)goBack
{
  // logic to be done before going back
  [self popViewControllerAnimated:secondViewController animated:TRUE];
}
+2

"". , " ".

0

Source: https://habr.com/ru/post/1713099/


All Articles