我有一个登录控制器,并且在成功登录后我想将一些字符串值传递给菜单页面。但是它不起作用。应用程序崩溃了。 我已经尝试过从下面的链接中获取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];
//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
答案 0 :(得分:1)
在您的应用程序委托中创建一个属性,例如NSString*
myString
。
然后从登录控制器和其他控制器访问它,如下所示:
[[UIApplication sharedApplication] delegate].myString
例如,您可以在登录控制器中设置myString
的值:
[[UIApplication sharedApplication] delegate].myString = @"value";
您可以在任何其他控制器中阅读:
NSLog(@"myString is: %@", [[UIApplication sharedApplication] delegate].myString);
此外,一些Cocoa风格提示:
AccountSummary_Button
等)大写。ViewAccountSummary_method
等。)你唯一应该利用的是课本身(Menu
等)。