ios - 从另一个 View Controller 导航栏加载 View Controller 的 UIWebView 中的请求 url

标签 ios objective-c xcode

我正在尝试

[webLoad loadRequest:[NSURLRequest requestWithURL:url]];

当从另一个 ViewController 中单击时在 ViewController 中

- (IBAction)actionClick:(id)sender { }

导航图标。

我使用了下面的代码,但它不起作用

=========================AnotherViewController============================

- (IBAction)actionClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self];
    [self performSegueWithIdentifier:@"toURL" sender:self];
}

=============================ViewController============================

    - (void)viewDidLoad {
          [[NSNotificationCenter defaultCenter] addObserver:self                                       
              selector:@selector(Notification:)
              name:@"Notification"
              object:nil];
    }
- (void) Notification:(NSNotification *) notification
{

       progressBar.progress = 0;
       progressBar.hidden = false;
       NSString *urlString = [[NSUserDefaults standardUserDefaults]stringForKey:@"urlSearch"];
       urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
       NSURL *url = [NSURL URLWithString:urlString];
       [webLoad loadRequest:[NSURLRequest requestWithURL:url]];

}

最佳答案

问题出在这段代码中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self];
[self performSegueWithIdentifier:@"toURL" sender:self];

您甚至在初始化 ViewController 之前就发布了一个通知。 我看不到您在代码中使用了任何 NSNotification。 因此,如果您只是希望 ViewController 在打开时加载一个 url,那么您可以简单地执行此操作。

- (void)viewDidLoad {
   [super viewDidLoad];
   [self loadUrl];
}
- (void)loadUrl
{
       progressBar.progress = 0;
       progressBar.hidden = false;
       NSString *urlString = [[NSUserDefaults standardUserDefaults]stringForKey:@"urlSearch"];
       urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
       NSURL *url = [NSURL URLWithString:urlString];
       [webLoad loadRequest:[NSURLRequest requestWithURL:url]];

}

关于ios - 从另一个 View Controller 导航栏加载 View Controller 的 UIWebView 中的请求 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556405/

相关文章:

objective-c - 通过文本字段上的自动布局调整字体大小并更改高度

c - 推荐学习C

ios - 如何为 iPhone 5 更新完全由图像组成的应用程序

ios - 如何修复主 Storyboard上不正确的(小) View

ios - 更改 UITextView 上的字体(或文本量)会使文本消失

ios - UITableView - 使用 Swift 注册类

objective-c - 内存泄漏、nsmutablearray 和自定义对象

objective-c - IOS - ObjC - 来自 NSString 的 JSONString

ios - 通过 Storyboard有效地传递 NSManagedObject

boolean 值的Objective-c dealloc