I have a UIViewController
that instantiates another UIViewController
(with NIB) and pushes it to the screen. I then try to set UILabel
and get UIWebView
to load URL. However it doesn't work! No errors! Interface looks just like in NIB and no programmatic measures to set UILabel
to a different @"TestString"
work.
Can someone point me to the issue? I just can't seem to resolve it.
Thank you.
Instantiated UIViewController - WebView.h:
#import <UIKit/UIKit.h>
@interface WebView : UIViewController {
}
@property (strong, nonatomic) IBOutlet UILabel *testLabel;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
- (void) openWeb: (NSString *) url;
@end
Instantiated UIViewController - WebView.m:
#import "WebView.h"
@interface WebView ()
@end
@implementation WebView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_testLabel = [[UILabel alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setTestLabel:(UILabel *)theTestLabel {
NSLog(@"Called set Label: %@", [theTestLabel text]);
_testLabel = theTestLabel;
}
- (void) openWeb: (NSString *) url {
NSLog(@"openWeb with url %@", url);
[_testLabel setText:url];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[[NSURL alloc] initWithString:url]];
[_webView loadRequest:urlRequest];
}
@end
Instantiating Part:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ScheduledClass *c = [classes objectAtIndex:[indexPath row]];
WebView *webView;
webView = [[WebView alloc] initWithNibName:@"WebView" bundle:nil];
UILabel *label = [[UILabel alloc] init];
label.text = @"Testing";
[webView setTestLabel:label];
[webView openWeb:@"http://meirz.net"];
[self.navigationController pushViewController:webView animated:YES];
}
Update: Confirmed connections
It's odd that more people don't have this issue.
The reason why you can't set the properties on these IBOutlets is that these UI objects haven't been initialised yet.
You've only initialised the ViewController by this stage.
So what you need to do is create a string that will be used in your label.
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) NSString *stringForLabel;
Set it here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebView *webView = [[WebView alloc] initWithNibName:@"WebView" bundle:nil];
webView.stringForLabel = @"My Label String";
[self.navigationController pushViewController:webView animated:YES];
}
Then:
- (void)viewDidLoad {
[super viewDidLoad];
self.label.text = self.stringForLabel;
}
Same applies if you're using storyboards with segues:
- (void)buttonPress:(id)sender {
[self performSegueWithIdentifier:@"mySegue" sender:self];
}
- (void)prepareForSegue(UIStoryboardSegue *)segue sender:(id)sender {
AController *myController = [segue destinationViewController];
myController.stringForLabel = @"My Label String";
}