简体   繁体   English

使用界面生成器设计视图

[英]designing a view with interface builder

I'm new to interface builder. 我是界面构建器的新手。

I'd like to design a view in interface builder. 我想在界面生成器中设计一个视图。
I know I can load the view via 'loadNibNamed'. 我知道我可以通过“ loadNibNamed”加载视图。

Now, suppose there will be two buttons(or two tap gesture recognizers) in the view, and when those buttons are touched, some functions need to fire. 现在,假设视图中将有两个按钮(或两个轻击手势识别器),并且当触摸这些按钮时,需要触发某些功能。
How do I connect(?) the touch to some functions of the viewController that I called the 'loadNibNamed' from? 我如何将触摸连接到我称为“ loadNibNamed”的viewController的某些功能?

The view will be a pop-up view ( PopUp view ) with two buttons(or tap gesture recognizers). 该视图将是带有两个按钮(或点击手势识别器)的弹出视图( PopUp视图 )。

You need to set up outlets from your interface builder objects (buttons) to your class. 您需要设置从接口构建器对象(按钮)到类的出口。

To do this, you need to make your class that calls "loadNibNamed" as a delegate class. 为此,您需要将调用“ loadNibNamed”的类作为委托类。 Then synthesize the button so it is a property. 然后合成按钮,使其成为属性。

Once you do that add the selector: 完成后,添加选择器:

 [button addTarget:self action:@selector(buttonPressed)

forControlEvents:UIControlEventTouchUpInside]; forControlEvents:UIControlEventTouchUpInside];

The better way would be to write a new class for that particular view. 更好的方法是写一个新的类为该特定视图。 Then if you want to catch the event in the class that called "loadNibNamed" you can set the target to something else: 然后,如果您想在名为“ loadNibNamed”的类中捕获事件,则可以将目标设置为其他内容:

[button addTarget:(UIMyClass)sender action:@selector(buttonPressed)
   forControlEvents:UIControlEventTouchUpInside];

There should be lots of tutorials out there :) - btw have you considered upgrading your xcode and using storyboards? 那里应该有很多教程:)-顺便说一句,您是否考虑过升级xcode和使用情节提要?

--EDIT:-- - 编辑: -

The problem happens because you are mixing coding with interface building. 发生问题是因为您将编码与接口构建混合在一起。 I think you still need to make a viewcontroller class for your new view, but you can change the target of the selector to the calling class. 我认为您仍然需要为新视图创建一个viewcontroller类,但是您可以将选择器的目标更改为调用类。

See more here about selectors: @selector and other class (Objective-C) 在此处查看有关选择器的更多信息: @selector和其他类(Objective-C)

There is a similar question here about pushing data to a new view programatically: 关于将数据以编程方式推送到新视图,这里存在一个类似的问题:

Can we pass a parameter to view did load or view will appear of other class from a class 我们可以传递参数以查看是否已加载或视图是否将出现在一个类的其他类中

If you're new to IB there are three steps. 如果您不熟悉IB,则需要执行三个步骤。

  1. Add the method name in your .h (header) file, ie 将方法名称添加到您的.h(头)文件中,即

    -(IBAction)someMethod:(id) sender; -(IBAction)someMethod:(id)发送者;

  2. Save the modified .h file. 保存修改后的.h文件。

  3. Open Interface Builder. 打开Interface Builder。

  4. Open the File Owner window under the Tools menu I believe. 我相信在“工具”菜单下打开“文件所有者”窗口。

  5. Cntrl+Click on the File Owner selection under the File Owner Window. Cntrl +单击“文件所有者”窗口下的“文件所有者”选项。 A black window should appear with a bunch of interface options under it. 将出现一个黑色的窗口,下面有许多接口选项。 A small black circle should appear next to someMethod 一个小黑圈应该出现在someMethod旁边

  6. Drag that small black circle (a blue line should appear) onto the button you want your method to connect to. 将那个黑色小圆圈(应该出现一条蓝线)拖到您要方法连接的按钮上。 Another menu should appear. 出现另一个菜单。 Choose an appropriate action (something like on touch or something like that). 选择适当的操作(例如触摸之类的操作)。 Repeat for the second button. 重复第二个按钮。

  7. In the IB for each button give them a tag in the attributes list under the Properties (I believe). 在IB中,每个按钮在“属性”下的“属性”列表中给它们一个标签(我相信)。 If you need the Properties window look under the Tools menu again. 如果需要“属性”窗口,请再次在“工具”菜单下查看。 Go to the Tag section and give each button a different tag. 转到标签部分,为每个按钮分配一个不同的标签。 (1, 2, 3 ... etc). (1、2、3 ...等)。

  8. Implement the method in the .m file. 在.m文件中实现该方法。 Make sure you differentiate your actions for which button the user selects, ie if (sender.tag == 1) {...} else if (sender.tag == 2) {...} 确保区分用户为哪个按钮选择的操作,即if(sender.tag == 1){...}否则if(sender.tag == 2){...}

Save and run. 保存并运行。

The end. 结束。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM