Reputation: 11
Im trying to
[webLoad loadRequest:[NSURLRequest requestWithURL:url]];
in a ViewController when clicked from AnotherViewController's
- (IBAction)actionClick:(id)sender { }
navigation icon.
i used below code, but its not working
=========================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]];
}
Upvotes: 0
Views: 41
Reputation: 31
I suggest you declare a property on your ViewController, i.e.
@property (nonatomic, strong) NSString *urlString;
Then pass it on prepare segue as mentioned here.
i.e.
if ([[segue identifier] isEqualToString:@"ViewControllerSegue"]) {
ViewController *vc = [segue destinationViewController];
vc.urlString = @“yourURL”; }
Then modify your loadURL to accept string, like so :
- (void)viewDidLoad {
[super viewDidLoad];
[self loadUrl:urlString];
}
- (void)loadUrl:(String *)urlString
{
progressBar.progress = 0;
progressBar.hidden = false;
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
[webLoad loadRequest:[NSURLRequest requestWithURL:url]];
}
Upvotes: 0
Reputation: 6963
Problem lies in this code:
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self];
[self performSegueWithIdentifier:@"toURL" sender:self];
You post a notification before ViewController is even initialised. I can't see you are making any use of NSNotification in your code whatsoever. So if you just want the ViewController to loads a url when opened up then you can just simply do this.
- (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]];
}
Upvotes: 1