[英]Can't set any IBOutlet
我有一個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];
}
更新:確認連接
奇怪的是,更多的人沒有這個問題。
您無法在這些IBOutlets上設置屬性的原因是,這些UI對象尚未初始化。
您僅在此階段初始化了ViewController。
因此,您需要做的是創建一個將在標簽中使用的字符串。
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) NSString *stringForLabel;
放在這里:
- (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];
}
然后:
- (void)viewDidLoad {
[super viewDidLoad];
self.label.text = self.stringForLabel;
}
如果將帶有故事的故事板一起使用,則同樣適用:
- (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";
}
我嘗試了所有現有的答案,但沒有找到任何有幫助的方法。 現在,我發現的是以下鏈接: 另一個UIViewController中的UIViewController的新實例:為什么不能設置實例變量?
據我了解(如果我錯了,請糾正我-這對我很重要)在實際加載視圖之前,我無法設置任何插座值!!!
這意味着如果您嘗試在之前將值設置為Outlets
- (void)viewDidLoad { [super viewDidLoad]; }
執行后,以上所有值將被[super viewDidLoad];
覆蓋[super viewDidLoad];
因此,任何值都不會反映任何更改。
據我所知,需要做的是-定義不是Outlet的變量,並為其分配所需的值。 在- (void) viewDidLoad { [super viewDidLoad]; }
- (void) viewDidLoad { [super viewDidLoad]; }
方法,在超級之后,您將上述定義的值分配給受尊重的插座。
請確認或糾正我的答案! 非常感謝你。
可能您尚未連接插座。 嘗試將CTRL
從Interface Builder中的UI控件拖動到View Controller。
另一個可能的解決方案是選擇控件,打開右面板的最后一個選項卡,然后在此處查找連接。
您可能需要連接IBOutlets。 正確連接后,它們在源代碼中應如下所示:每個連接的IBOutlet的左側小灰點都應填充。
選中此選項,這將對您有所幫助。您需要在文件中設置FileOwner 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.