0

我正在使用自定义 URL 方案打开我的应用程序,然后获取链接并在方法中运行。但我无法真正运行该方法。

例如,我无法加载 Web 视图或更改标签或文本字段。那么如何加载网页视图和更改标签?

AppDelegate.m

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {    
    if (!url) {  return NO; }
    NSString *URLopen= [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ViewController *vc = [[ViewController alloc]init];
    vc.URLschemeLink = URLopen;
    [vc URLscheme];   
    return YES;
}

ViewController.h

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> {
    NSString *URLschemeLink;
}

-(void)URLscheme;

@end

ViewController.m

@implementation ViewController
@synthesize URLschemeLink;

-(void)URLscheme {
    //for example:
    label.text = @"Hello"; //nothing will happen
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]]; //Nothing will happen

    NSLog ("Method Works Perfect"); //will happen

    UIAlertView *alert = [[UIAlertView alloc]
                               initWithTitle:@"Title:"
                                     message:[NSString stringWithFormat:@"%@", URLSchemeLink]
                                    delegate:nil
                           cancelButtonTitle:@"OK"
                           otherButtonTitles:nil];
    [alert show];
    //UIAlertView will work perfectly and show the URLschemeLink from AppDelegate.
}

好的 无论如何如何加载标签/webview?我测试从应用程序委托传递一个名为 runURLscheme (=true) 的布尔值。然后我在 ViewDidLoad 中写道:

if(runURLscheme==true) {
    [self URLScheme];
}

但这不起作用,它不会运行 URLscheme 方法。无论如何,我如何加载标签/网络视图?

4

1 回答 1

0

与视图控制器关联的主视图通常是延迟加载的。仅仅因为视图控制器被初始化,并不意味着它已经加载了它的主视图。您的视图只有viewDidLoad在被调用后才能安全访问。

一般来说,这种模式效果很好:

  • 独立于显示它的任何视图存储您的数据。
  • 当您的自定义 URL 方案运行时,更新此数据。
  • 在您的视图控制器上有一个方法,可以根据这些数据更新其视图。
  • 从 调用此方法viewDidLoad

如果在你的视图已经加载后这些数据有可能被更新(例如,如果你从网络接收数据),那么使用通知或 KVO 再次调用你的视图更新方法。

于 2013-01-26T18:18:35.187 回答