UITextView 成员变量在我的 UIViewController 中始终为零
在我的笔尖中,我有一个 UITextView 组件。
在我的 UIViewController 中,我有一个 UITextView 成员字段,并且我使用 IB 来确保两者已连接(至少我认为我是通过从“文件所有者”拖动来做到这一点的) ”并将其放在 IB 中的 UITextView 上并选择我的成员变量)。
现在,当我通过 setText:
更新文本时,UITextView
仍然保持空白。
当我闯入我的代码时,成员变量(称为 textView
)为 nil
。
所以我猜我没有正确使用IB。如何确保此变量已连接到 UITextView
图形元素?
这是代码
// 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
连接插座后保存 nib 文件,构建项目并运行,如果已连接,它应该可以工作
save nib file after connecting outlet, build project and run, it should work if it's connected
我看不到您正在调用 setText,但我认为您尝试在初始化程序中执行此操作。如果您以编程方式创建 GUI,或者使用其他 viewWill/viewDid... 方法,则在使用 XIB 或 loadView 的情况下,GUI 代码不应在 viewDidLoad 之前完成。
原因是因为视图在实际需要之前才加载,这称为延迟加载。你应该谷歌一下以获取更多信息。
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.