【问题标题】:i am not able to pass value from one view controller to another view controller我无法将值从一个视图控制器传递到另一个视图控制器
【发布时间】:2012-11-16 14:53:21
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c uiview uiviewcontroller


    【解决方案1】:

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

    -(IBAction)save:(id)sender 
    { 
        nextview *admin = [[nextview alloc] init];
    
        admin.fetchname = name.text; 
    
        [self presentModalViewController:admin animated:YES];  
        [admin release];
    }
    

    【讨论】:

      【解决方案2】:
         -(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 是为属性选择的非常糟糕的名称。

      【讨论】:

        【解决方案3】:

        你可以做这样的事情。 您可以在 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];
                  }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-11
          相关资源
          最近更新 更多