簡體   English   中英

從AppDelegate將變量傳遞到視圖

[英]Passing variables to the view from AppDelegate

我在AppDelegate中打開視圖

與:

AppDelegate

PushDetail *pushdtls = [[PushDetail alloc] initWithNibName:nil bundle:nil];   
pushdtls.seminarurl = [NSURL URLWithString:resourcePathURL]; /passing URL
[self.window presentModalViewController:pushdtls animated:YES];

PushDetail.h

@property (nonatomic) NSURL *seminarurl;

PushDetail.m

- (void)viewDidLoad
{
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:seminarurl];
    [pushDetails loadRequest:requestObj];
}

但是網絡視圖是空的...我做錯了什么?

第二個問題是如何關閉我打開的視圖?

- (IBAction) closeNews{
    //[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    //[self release];
    [self dismissModalViewControllerAnimated:YES];
}

不起作用:(

您可以在視圖控制器中實現UIWebViewDelegate協議,並自己獲取結果/錯誤消息。

didFailLoadWithError有關更多信息,請參見UIWebViewDelegate協議參考

也許只是您URL上的錯字。

例:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"oops, failed with error: %@", error);
}

您尚未指定pushDetails是什么,但我假設它是PushDetail控制器上UIWebview的IBOutlet。 我的猜測是您忘記了將pushDetails出口綁定到nib文件中的webview。

您可以創建一個包含所有全局變量的文件,將其命名為GlobalVariables.h和.m。 在GlobalVariables.h文件中添加此

extern NSURL *seminarurl;

@interface GlobalVariables

@property (retain, nonatomic) NSURL *seminarurl;    

@end

並在GlobalVariables.m文件中添加

#import "GlobalVariables.h"

@implements GlobalVariables

@synthesize seminarurl;

NSURL *seminarurl; // You could add what your URL is here as well you would just use '='

@end

因此,當您要訪問它或為其分配變量時,就像訪問或分配任何其他變量一樣。 只需記住將“ GlobalVariables.h”導入到您正在其中使用的任何.m文件中即可。

  1. 保留您的財產。 例如>> @property (nonatomic,retain)

  2. 如果url是您的應用程序委托中的一個屬性..您可以像這樣訪問它

    [UIApplication SharedApplication] .delegate。您的屬性名;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM