[英]What is the simplest way to add outlet to my view controller?
When I add new view, I always do same and boring work. 当我添加新视图时,我总是做同样无聊的工作。
If I add UIWebView, I would do follows, 如果添加UIWebView,我将执行以下操作:
[ViewController.h] [ViewController.h]
@interface ViewController : UIViewController
{
UIWebView *_webView;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@end
[ViewController.m] [ViewController.m]
@synthesize webView = _webView;
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
}
- (void)dealloc
{
[_webView release];
[super dealloc];
}
What if I should add 3 labels and 2 buttons? 如果我应该添加3个标签和2个按钮怎么办? What if I have to add textview and some imageviews?
如果我必须添加textview和一些imageviews怎么办?
Don't you think it is boring? 你不觉得很无聊吗? I would like to listen to your idea.
我想听听你的想法。
I hope there will be more easy and simple way to add outlet to the code. 我希望将有更多简便的方法来添加代码出口。 Does anybody have a good idea?
有人有个好主意吗? :)
:)
When using Interface Builder
, If you select an Object drag it's reference to your header, you'll see a popup where you can name it so theres less typing for you to do. 使用
Interface Builder
,如果选择“对象”拖动,它是对标题的引用,您将看到一个弹出窗口,您可以在其中命名它,从而减少了键入操作。 As seen below: 如下图所示:
This will automatically declare the IBOutlet UIButton *myButton
for you, and insert the release
and nil
code into dealloc
and viewDidUnload
methods. 这将自动为您声明
IBOutlet UIButton *myButton
,并将release
和nil
代码插入dealloc
和viewDidUnload
方法中。
Same method also works for actions, as seen below. 相同的方法也适用于操作,如下所示。
Once you Connect
it will automatically insert the new Action -(IBAction)cancelSelected:(id)sender
into your @implementation
class. 一旦
Connect
,它将自动将新的-(IBAction)cancelSelected:(id)sender
插入您的@implementation
类。
Point being, all that's boring for you to do can be done in 2
Reference connections, and inputting data into 2
fields. 要点是,您要做的所有无聊工作都可以在
2
参考连接中完成,并将数据输入到2
字段中。 :) :)
Hope this helps! 希望这可以帮助!
In Xcode 4, you can simply control drag from a UI element to the .h/.m file. 在Xcode 4中,您可以简单地控制从UI元素到.h / .m文件的拖动。 If you drag to the .h, Xcode will create a property for you and synthesize that in the corresponding .m, if you drag to the .m, Xcode will stub out an IBAction method for you.
如果拖动到.h,则Xcode将为您创建一个属性,并在相应的.m中对其进行综合;如果拖动到.m,则Xcode将为您存入IBAction方法。
Also, I would recommend switching to ARC, to avoid having to worry about memory management. 另外,我建议切换到ARC,以避免担心内存管理。
If you are having xcode 4.0 you can create outlet property by drag and drop. 如果您使用的是xcode 4.0,则可以通过拖放创建奥特莱斯属性。 Follow the steps:
按照步骤:
dealloc
and viewDidUnload
. dealloc
和viewDidUnload
的代码。 Hope this help. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.