嗨我有问题,我无法将值从一个视图控制器传递到另一个视图控制器通过按钮我实现当我点击按钮其他视图出现在iPhone屏幕上,但我设置的值不显示这是按钮代码
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc]init];
[self presentModalViewController:admin animated:YES];
if (admin.view)
{
admin.fetchname = name.text;
}
[admin release];
}
这是nextview.h文件
#import <UIKit/UIKit.h>
@interface nextview : UIViewController
{
UILabel *getname;
NSString *fetchname;
}
@property (nonatomic,retain) IBOutlet NSString *fetchname;
@property (nonatomic,retain) IBOutlet UILabel *getname;
@end
这是nextview.m文件
#import "nextview.h"
#import "ViewController.h"
@implementation nextview
@synthesize getname,fetchname;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
getname.text = self.fetchname;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
答案 0 :(得分:1)
在您有机会分配NSString值之前调用viewDidLoad方法。 这就是你在UIButton中看不到文字的原因。 试试这个:
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc] init];
admin.fetchname = name.text;
[self presentModalViewController:admin animated:YES];
[admin release];
}
答案 1 :(得分:0)
-(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc]init];
[self presentModalViewController:admin animated:YES];
if (admin.view)
{
admin.fetchname = name.text;
}
[admin release];
}
分配名称后立即释放nextview实例。这甚至无法奏效。
顺便说一下,getname和fetchname是属性的错误选择名称。
答案 2 :(得分:0)
你可以这样做。 您可以在nextView.h中实现以下代码
NSString *fetchData;
也属性并合成此
@property(nonatomic, retain) NSString *fetchData;
在按下按钮的代码
上实现此功能 -(IBAction)save:(id)sender
{
nextview *admin = [[nextview alloc] init];
admin.fetchData = name.text;
[self presentModalViewController:admin animated:YES];
[admin release];
}