0

I need to make a button to do something on another page. I know how to code a button to make wanted things on the same page(same viewcontroller) also make that button to open another page(another viewcontroller) but how can I make it both at the same time ?

Here is an example for a simple calculator.

  • Open the program
  • Enter two numbers
  • Click the button
  • SecondPage comes up and shows the result from the first page

Is it something about delegates? Please explain.

I receive some answers and thanks for that lets make it simple and make that button to write something on a label which is in the second page can you write that code too its simple a button at the first page will write something to a label on second page. First view controllers name is ru1 second viewcontrollers name is ru2

Also can you explain me where to write what I am noob and I have hard time understanding what you say ?

3 Answers3

1

You can create a selector that does it...

[myButton addTarget:self action:@selector(mySelector) forControlEvents:UIControlEventTouchUpInside];

your selector

- (void)mySelector {
    myNewViewController *secondController = [[myNewViewController alloc] init];
    [secondController setMyProperty:@"SOME_VAR"];
    [self.navigationController pushViewController:secondController animated:YES];
}

in the secondController.h

@property (nonatomic, strong) NSString *myProperty;

in the secondController.m

@synthesize myProperty;

in your second (ru2) controller in the -(void)viewDidLoad:

UILabel *lblSecond = [[UILabel alloc] initWithFrame:CGRect(10, 10, 20, 100)];
[lblSecond setText:myProperty];
[self.view addSubview:lblSecond];
NSPunk
  • 502
  • 4
  • 14
  • I understand the the thing you say so so but lets make it simple and make that button to write something on a label which is in the second page can you write that code too its simple a button at the first page will write something to a label on second page. First view controllers name is ru1 second viewcontrollers name is ru2 Also can you explain me where to write what ? – user1546565 Jul 23 '12 at 18:36
  • @user1546565 Edited with viewDidLoad. – NSPunk Jul 23 '12 at 18:42
  • I created a label with some random size, and took that property to set the text for it. Quite simple. – NSPunk Jul 23 '12 at 18:50
  • But I want that button to set the text for the label which is in another page(viewcontroller) dont I need to connect them ? – user1546565 Jul 23 '12 at 18:52
  • Hmmm, i believe you need some more information than we gave to you. Perhaps if you see this link, it can help you more. http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – NSPunk Jul 23 '12 at 18:55
  • I read the whole page but still I didnt understand I dont want to pass a bool just simple words cant you tell me how to make a button to write on a label which is on the other page ? Ru1 first viewcontroller which has button on it Ru2 is the secound viewcontroller which has label on it – – user1546565 Jul 23 '12 at 20:56
  • @user1546565 if you can't change a bool to a string, even reading that page... your problem is a lot bigger. Try to start with some beginners tutorial. – NSPunk Jul 24 '12 at 12:04
0

In header file of second view controller add variables, that you want to pass to it:

@interface ATSecondViewController : UIViewController <UINavigationControllerDelegate>{
    IBOutlet UITableView *_tableView;
    NSFileManager *fileManager;
    NSString *documentsDir;
    IBOutlet UILabel *top_bar;
}

In method where you are pushing that new controller they will be accessible with something like this:

ATSecondViewController *detailViewController = [[ATSecondViewController alloc] initWithNibName:@"ATBuyTripViewController" bundle:nil];
    detailViewController.documentsDir = @"SOME DIR";

If you synthesize that variables in SecondViewController they can be accessed through your code.

Sebastian Łuczak
  • 1,116
  • 8
  • 19
  • I understand the the thing you say so so but lets make it simple and make that button to write something on a label which is in the second page can you write that code too its simple a button at the first page will write something to a label on second page. First view controllers name is ru1 second viewcontrollers name is ru2 Also can you explain me where to write what ? – user1546565 Jul 23 '12 at 18:36
0

If the 'other' view controller exists at the time of button tap (e.g., it is somewhere in the navigation stack) you can post a notification. The 'other' view controller must do this on init:

[[NSNotificationCenter defaultCenter] addObserver:self name:YourCustomNotificationName object:nil];

And, on dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self];

On button tap (current view controller):

[[NSNotificationCenter defaultCenter] postNotificationName: YourCustomNotificationName object:self userInfo:someCustomDictionary];

'YourCustomNotificationName' is an NSString you must define somewhere visible to both view controllers.

Optionally, if you are creating the button programatically, you can use the other view controller (instead of self) when calling 'addTarget:action:forControlEvents'. If you are using IB, I don't know... There's the whole 'file's owner' thing...

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189