【问题标题】:When and where are controller's properties supposed to be set?应该在何时何地设置控制器的属性?
【发布时间】:2011-01-16 13:31:51
【问题描述】:

这一次,我想知道我应该什么时候设置我的属性...

我有一个导航栏,用于推送新控制器(控制 Web 视图):

NewsViewController *webViewController = [[NewsViewController alloc] init]; // I create my controller

webViewController.urlText = @"http://www.google.fr"; // I set the property

InfonulAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 

[delegate.newsNavController pushViewController:webViewController animated:YES];

[webViewController release];

我不知道为什么,但是下面的代码不起作用:

- (void)viewDidLoad { //viewDidLoad from my webViewController

   [super viewDidLoad];

   //Create a URL object.
   NSURL *url = [NSURL URLWithString:urlText];
   //URL Requst Object
   NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

   //Load the request in the UIWebView.
   [webView loadRequest:requestObj];
}

我只想创建一个 UIWebView 但我需要为控制器提供要使用的 URL!

知道需要在何时何地设置我的 urlText 属性吗?!?

干杯,

高蒂尔

【问题讨论】:

    标签: iphone properties uiwebview uinavigationcontroller


    【解决方案1】:

    您正确使用属性吗?喜欢

    @property(nonatomic,retain) NSString *urlText;
    

    如果是这样,请尝试使用这样的自定义 init 方法;

    -(id)initWithUrl:(NSString *)url
    {
         if(self = [super init])
         {
              self.urlText = url;
    
         }
         return self;
    }
    

    不要忘记在 dealloc 中释放 urlText。现在使用;

    NewsViewController *webViewController = [[NewsViewController alloc] initWithUrl:@"someUrl"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多