Hi all!
I have been trying to figure out how to pass data between tab bar views, but no success. Put name in the Textfield and push a button on firstview, however a label on secoundview doesn't update.
I found example codes for OS 4.3, but it doesn't work on OS 5 with storyboard... Here is a storyboard screenshot and codes. What's wrong with them? Please help!
FirstViewController.h
Code:
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController <UITextFieldDelegate> {
NSString *fullName;
}
@property (nonatomic, retain) NSString *fullName;
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
- (IBAction)nextVCButton:(id)sender;
@end
FirstViewController.m
Code:
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
@synthesize nameTextField, fullName;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)nextVCButton:(id)sender {
SecondViewController *secView = [[SecondViewController alloc] initWithNibName:@"SecondViewController"
bundle:nil];
[secView setFullName:fullName];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *ivc = [segue destinationViewController];
ivc.fullName = fullName;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
self.fullName = [textField text];
return YES;
}
@end
SecondViewConroller.h
Code:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController{
NSString *fullName;
UILabel *nameLabel;
}
@property (retain, nonatomic) NSString *fullName;
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@end
SecondViewController.m
Code:
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize nameLabel, fullName;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
nameLabel.text = fullName;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// [self.nameLabel setText:fullName];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end