开发者

how to pass a string value from one controller to another

I have a login controller ,and after the successful login i want to pass some string value to the menu page.however it does not work.the application crashes. I have tried possible suggesstion of Ihuk and SAM from the link below

how to pass a string value from one view controller to another view controller

loginController.h:

#import <UIKit/UIKit.h>
@class RootViewController;
@class Menu;

@interface LoginController : UIViewController {
  UIButton *login_Button;
  UITextField *username_TextField;
  UITextField *password_TextField;
  RootViewController *mc1;
  UINavigationController *navigationController;
  Menu *mv1;
}

@property(nonatomic,retain) IBOutlet UIButton *login_Button;
@property(nonatomic,retain) IBOutlet UITextField *username_TextField;
@property(nonatomic,retain) IBOutlet UITextField *password_TextField;
@property(nonatomic,retain) RootViewController *mc1;
@property (nonatomic, retain) IBOutlet 
         UINavigationController *navigationController;
@property(nonatomic,retain)Menu *mv1;

- (IBAction)Login_Method:(id)sender;
-(id)initWithUserName:(NSString *)name ;
@end

loginController.m

#import "LoginController.h"
#import "Menu.h"
#import "ViewController.h"
#import "RootViewController.h"

@implementation LoginController

@synthesize mc1,mv1;
@synthesize login_Button,username_TextField,password_TextField;
@synthesize navigationController;


// Implement viewDidLoad to do additional setup after 
// loading the view, typically from a nib.
- (void)viewDidLoad {   
  if (![self.navigationController isNavigationBarHidden])
    [self.navigationController setNavigationBarHidden:YES animated:NO];
//[self presentModalViewController:navigationController animated:YES];
}

- (void)didReceiveMemoryWarning {
  // Releases the view if it doesn't have开发者_运维技巧 a superview.
  [super didReceiveMemoryWarning];

  // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (IBAction)Login_Method:(id)sender
{           
  Menu *mv2 = [[Menu alloc] initWithUserName:@"Menu" bundle:nil];
  //mv2.l1.text=@"aa"; //i tried this, but not work,so created initWithUserName
  self.mv1=mv2;
  [self presentModalViewController:mv1 animated:YES];
//  [RootViewController release];
}

-(id)initWithUserName:(NSString *)name 
{
  self = [super init];
  if (nil == self) {
    return nil;
  }

  // display or store login info somewhere
  [mv1.l1 setText:name];

  return self;
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {  
  [theTextField resignFirstResponder];
  return YES;
}

- (void)dealloc {
  [username_TextField release];
  [password_TextField release];
  [super dealloc];
}

@end

Menu.h

#import <UIKit/UIKit.h>

@class Menu;
@interface Menu : UIViewController {    
  UILabel *l1;
  UIButton *AccountSummary_Button;
  UIButton *PayOffQuote_Button;
  UIButton *PayBill_Button;
  UIButton *Logout_Button;
  UINavigationController *nv1;
}

@property(nonatomic,retain) IBOutlet UILabel *l1;
@property(nonatomic,retain) IBOutlet UIButton *AccountSummary_Button;
@property(nonatomic,retain) IBOutlet UIButton *PayOffQuote_Button;
@property(nonatomic,retain) IBOutlet UIButton *PayBill_Button;
@property(nonatomic,retain) IBOutlet UIButton *Logout_Button;
@property (nonatomic, retain) IBOutlet UINavigationController *nv1;

-(IBAction)ViewAccountSummary_method:(id)sender;
-(IBAction)ViewPayOffQuote_method:(id)sender;
-(IBAction)ViewPayBill_method:(id)sender;
-(IBAction)Logout_method:(id)sender;

@end

Menu.m


Make a property in your application delegate, for example, an NSString* called myString.

Then access it from the login controller and other controllers like so:

[[UIApplication sharedApplication] delegate].myString

You could, for example, set myString's value in the login controller:

[[UIApplication sharedApplication] delegate].myString = @"value";

You could read it in any other controller:

NSLog(@"myString is: %@", [[UIApplication sharedApplication] delegate].myString);

Additionally, some Cocoa style tips:

  1. Do not capitalize class members (AccountSummary_Button etc.)
  2. Do not capitalize class methods (ViewAccountSummary_method etc.)

The only thing you should capitalize are the classes themselves (Menu etc.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜