0

嗨,我有一个问题,当我单击按钮时,我无法通过按钮将值从一个视图控制器传递到另一个视图控制器,其他视图出现在 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
4

3 回答 3

1

viewDidLoad 方法在您有机会分配 NSString 值之前被调用。这就是为什么您在 UIButton 中看不到文本的原因。尝试这个 :

-(IBAction)save:(id)sender 
{ 
    nextview *admin = [[nextview alloc] init];

    admin.fetchname = name.text; 

    [self presentModalViewController:admin animated:YES];  
    [admin release];
}
于 2012-11-16T15:57:20.247 回答
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];
          }
于 2012-11-16T17:07:08.043 回答
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 是非常糟糕的属性名称。

于 2012-11-16T15:01:53.733 回答