【问题标题】:how to set the property of the ViewController before presenting it as presentModalViewController?如何在呈现为 presentModalViewController 之前设置 ViewController 的属性?
【发布时间】:2012-03-26 07:59:06
【问题描述】:

我这样展示视图控制器:

FBViewController *fbViewController =[[FBViewController alloc] initWithNibName:@"FBViewController" bundle:nil];
fbViewController.label.text=@"hello"; // I set the value of the property label which is the outlet
[self presentModalViewController:fbViewController animated:YES];

FBViewController.h:

#import <UIKit/UIKit.h>

@interface FBViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

FBViewController.m:

...
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@", self.label.text); // Here instead of "hello" i get the value which was in nib file.

}
...

问题是如何设置标签的值?

【问题讨论】:

    标签: xcode initialization uilabel presentmodalviewcontroller outlet


    【解决方案1】:

    您可以通过 NSString 检索文本来分配标签 modalview 控制器:

    FBViewController *fbViewController =[[FBViewController alloc]   initWithNibName:@"FBViewController" bundle:nil];
     fbViewController.labeltext=@"Your Text";
     [self presentModalViewController:fbViewController animated:YES];
    

    FBViewController.h

    @interface FBViewController : UIViewController {
    
     NSString *labeltext;
    
    }
    
    @property (nonatomic, retain) NSString *labeltext;
    

    并在 FBViewController.m 中使用视图加载方法

    - (void)viewDidLoad {
      label1.text=labeltext;
    }
    

    【讨论】:

    • 感谢您的回答。你知道为什么我的代码不起作用吗?为什么不能直接设置outlet的属性?
    • 因为在 Modal View Controller Showing 和 label text set default Text 上重新生成控件...!
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 2012-03-01
    • 2019-03-28
    • 2016-07-27
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多