UITextView成员变量在我的UIViewController总是nil [英] UITextView member variable is always nil in my UIViewController
问题描述
在我的笔记本中,我有一个 UITextView
组件。
在我的 UIViewController
我有一个 UITextView
成员字段,我已经使用IB,以确保两个连接(至少我认为我是通过拖动Files Owner,并将其放在IB中的 UITextView
上,并选择我的成员变量)。
我通过 setText:
更新文本, UITextView
仍然为空白。
当我打破我的代码,成员变量( textView
)是 nil
p>
所以我猜我没有正确使用IB。如何确保此变量连接到 UITextView
图形元素?
p>
//我的界面看起来像
#import< UIKit / UIKit.h>
@interface DetailsController:UIViewController
{
UITextView * textView;
}
@property(nonatomic,retain)IBOutlet UITextView * textView;
@end;
//和实现
#importDetailsController.h
@implementation DetailsController
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
//自定义初始化
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
//如果没有超级视图,则释放视图。
[super didReceiveMemoryWarning];
//释放所有未使用的缓存数据,图像等。
}
#pragma mark - 查看生命周期
- (void)viewDidLoad
{
[super viewDidLoad];
//从其nib加载视图后进行任何其他设置。
}
- (void)viewDidUnload
{
[super viewDidUnload];
//释放主视图的任何保留的子视图。
//例如self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//返回YES支持的方向
return(interfaceOrientation == UIInterfaceOrientationPortrait) ;
}
//按下完成按钮
- (IBAction)done
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)setDetails:(NSString *)details withQuote:(NSString *)quote
{
NSLog(@%@,textView) ;
[textView setText:detail];
}
@end
我看不到agere你正在调用setText,但我认为你尝试在初始化程序。在使用XIB或loadView的情况下,如果你以编程方式创建GUI,或者其他viewWill / viewDid ...方法,GUI代码不应该在viewDidLoad之前完成。
原因是因为视图在实际需要之前没有加载,所以称为延迟加载。您应该向Google索取更多信息。
In my nib, I have a UITextView
component.
In my UIViewController
I have a UITextView
member field and I have used IB to make sure that the two are connected (at least I think I did that by dragging from "Files Owner" and dropping it on the UITextView
in IB and selecting my member variable).
Now, when I update the text via setText:
, the UITextView
still remains blank.
When I break into my code, the member variable (called textView
) is nil
.
So I am guessing I have not used IB correctly. How can I make sure that this variable is connected to the UITextView
graphic element?
Here is the code
// My interface looks like
#import <UIKit/UIKit.h>
@interface DetailsController : UIViewController
{
UITextView *textView;
}
@property (nonatomic, retain) IBOutlet UITextView *textView;
@end;
// and the implementation
#import "DetailsController.h"
@implementation DetailsController
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
// used pressed the "Done" button
- (IBAction)done
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)setDetails: (NSString *)detail withQuote:(NSString *)quote
{
NSLog(@"%@", textView);
[textView setText:detail];
}
@end
I can't see agere you are calling setText, but I think that you try to do it in the initializer. GUI code should not be done before the viewDidLoad in the case of using XIB's or loadView if you make your GUI programatically, or the other viewWill/viewDid... methods.
The reason is because the views are not loaded before they are actually needed, it is called lazy loading. You should Google that for more info.
这篇关于UITextView成员变量在我的UIViewController总是nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!