简体   繁体   English

向我的视图控制器添加插座的最简单方法是什么?

[英]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,我将执行以下操作:

  • add codes to header file. 将代码添加到头文件。 ( declare, property ) (声明,财产)
  • add codes to source file. 将代码添加到源文件。 ( synthesize, viewDidUnload, dealloc ) (synthesize,viewDidUnload,dealloc)
  • add UIWebView in IB and connect to the outlet in File's Owner. 在IB中添加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 ,并将releasenil代码插入deallocviewDidUnload方法中。

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: 按照步骤:

  1. open the xcode 打开xcode
  2. open you nib file where you need to create an outlet. 在需要创建插座的位置打开您的笔尖文件。
  3. Click on the middle tab of the Editor which at right up corner. 单击编辑器的中间选项卡,该选项卡位于右上角。 It will open a new file adjacent to your nib. 它将打开一个与您的笔尖相邻的新文件。
  4. Make sure it is the .h file of the controller where you want to create the outlet. 确保它是要在其中创建出口的控制器的.h文件。
  5. Now select the control, right click on it and drag to the .h file. 现在选择控件,右键单击它并拖动到.h文件。 Name the outlet. 为出口命名。 Thats it. 而已。 It will create you a property and it will synthesize it automatically. 它将为您创建一个属性并自动对其进行合成。
  6. It will also insert the code for dealloc and viewDidUnload . 还将插入deallocviewDidUnload的代码。

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图控制器添加到视图层次结构的正确方法是什么? - What's the proper way to add a view controller to the view hierarchy? 在视图控制器宽度上水平均匀分布UIButton的最简单方法是什么? - Simplest way to evenly distribute UIButtons horizontally across width of view controller? 另一个视图控制器的呼叫出口 - Call Outlet of another view controller 在iOS 8中,什么是在视图控制器中除按钮之外的所有内容上添加手势识别器而不添加其他按钮的正确方法? - In iOS 8 what is the correct way to add a gesture recognizer on everything in my view controller except a button, without adding an additional button? 以编程方式加载View Controller的插座视图 - Loading View Controller's Outlet view Programmatically 创建“添加”视图控制器的最佳方法 - Best Way To Create an “Add” View Controller 将子视图添加到UIImageView,它是IB中UIViewController的视图出口 - Add a subview to a UIImageView that is the view outlet of a UIViewController in IB 这是管理我的视图控制器堆栈的好方法吗? - Is this a good way to manage my view controller stack? 在根UIView中滚动所有控件的最简单方法是什么? - What's the simplest way to make all my controls in my root UIView scroll? 如何在 iPhone SDK 中使用标签栏 controller 设置查看出口 - How to set view outlet using tab bar controller in iPhone SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM