如何将字符串值从一个控制器传递到另一个控制器
问题描述:
我有一个登录控制器,成功登录后,我想将某个字符串值传递给菜单页。但是它不起作用。应用程序崩溃。 我试图Ihuk和SAM的可能的宝贵意见,从下面的链接如何将字符串值从一个控制器传递到另一个控制器
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];
//[email protected]"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
答
在您的应用程序委托中创建一个属性,例如NSString*
,称为myString
。
然后从登录控制器和其他控制器访问它,像这样:
[[UIApplication sharedApplication] delegate].myString
你可以,例如,设置在登录控制器myString
的价值:
[[UIApplication sharedApplication] delegate].myString = @"value";
你可以阅读它在任何其他控制器:
NSLog(@"myString is: %@", [[UIApplication sharedApplication] delegate].myString);
另外,一些谷碧OA风格提示:
- 不要利用类成员(
AccountSummary_Button
等) - 不要利用类的方法(
ViewAccountSummary_method
等)
你应该利用的唯一的事情是类本身( Menu
等)。
使用4个空格缩进代码,以使其格式正确。阅读http://stackoverflow.com/editing-help以获取更多信息(也可通过点击答案输入字段上方的橙色问号来访问) – outis 2009-11-07 17:36:03
在模拟器中运行它时,请转到运行,控制台。然后继续登录并让应用程序崩溃。控制台底部记录的错误是什么? – marcc 2009-11-07 19:02:39
这是一个重复的:http://stackoverflow.com/questions/1692984/pass-a-string-value-from-one-view-controller-to-another – 2009-11-07 19:19:39