【发布时间】:2014-07-31 17:16:40
【问题描述】:
我有一个UIViewController 实例化另一个UIViewController(带有NIB)并将其推送到屏幕上。然后我尝试设置UILabel 并让UIWebView 加载URL。然而它不起作用!没有错误!界面看起来就像在 NIB 中一样,没有编程措施将 UILabel 设置为不同的 @"TestString" 工作。
有人可以指出我的问题吗?我似乎无法解决它。
谢谢。
实例化 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
实例化 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
实例化部分:
- (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];
}
更新:已确认连接
【问题讨论】:
-
_testLabel = [[UILabel alloc] init];你重新初始化了一个由笔尖设置的标签。[webView setTestLabel:label];你初始化了一个新标签,并将 web 视图的标签设置为此。这是您尝试删除默认标签的两倍。
标签: ios xcode uiviewcontroller uikit iboutlet