0

how to navigate to another page if my start page only has a button and textfield.On click of the button it has take the textfield data to another page and diplay it.I want to use navigation controller to return back.

slaveCoder
  • 519
  • 2
  • 17
  • 46

3 Answers3

1

if you use storyboard: name the segue between your first viewcontroller and the next viewcontroller in your storyboard and add the following method to your first viewcontroller.m:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"YourSegue"]){
         NextViewController *nvc = (NextViewController *)[segue destinationViewController];
         nvc.aString = self.textField.text;
     }
}
thorb65
  • 2,696
  • 2
  • 27
  • 37
  • Even if i add as many view controllers in my storyboard,there is only one viewcontroller.m/viewcontroller.h file present. Is it the way it should be? I am new to ios programming.. – slaveCoder Nov 26 '13 at 04:21
  • so whats the diffrence between my answer and this answer? – BhavikKama Nov 27 '13 at 05:32
0
ViewController * myVC = [[ViewController alloc] initWithNibName:nil bundle:nil];
myVC.someProperty = someValue;    // Pass your data here
[self.navigationController pushViewController:myVC animated:YES];

And in your ViewController class :

.h

@interface ViewController : UIViewController {
    NSString * _someProperty;
}
@property (nonatomic, retain) NSString * someProperty;
@end

.m

@implementation ViewController
@synthesize someProperty = _someProperty;

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"My Data == %@", _someProperty);
    }
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0

let say your first page is Firstviewcontroller and second page is Secondviewcontroller.

in Secondviewcontroller.h file

@property(strong,nonatomic)NSString *textfieldstring;

then in Secondviewcontroller.m

@synthesize *textfieldstring;

now in Firstviewcontroller on the click method of button just write this

//initialize secondviewcontroller object
secondviewcontroller *secondview=[[secondviewcontroller alloc]initWithNibName:@"secondviewcontroller" bundle:nil];

//Assign the textfield value to property of secondview
secondview.textfieldstring= yourtextfield.text;

//navigate to secondview controller
        [self.navigationController pushViewController:secondview animated:YES];

now in - (void)viewDidLoad method of Secondviewcontroller

you can get the textfield value in textfieldstring property then use this NSString value wherever you want.

if you using storyboard then you need to implement this method in the Firstviewcontroller.m file

  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"identifierofsecondviewcontroller"]) {

            Secondviewcontroller *secondview = segue.destinationViewController;
            secondview.textfieldstring= yourtextfield.text;
        }
    }

//and this thing you still need to write in Secondviewcontroller in Secondviewcontroller.h file

@property(strong,nonatomic)NSString *textfieldstring;

then in Secondviewcontroller.m

@synthesize *textfieldstring;

this is the tutorial that will help you definately http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

still if any query then feel free to ask me. Hope it helps...

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • Thanks for the help.but i'm using xcode 4.5 storyboards and new to ios programming. Can you explain this with storyboard because there is no nib file here. – slaveCoder Nov 26 '13 at 04:14