简体   繁体   English

将一个IBOutlet与不同的对象一起使用

[英]Using one IBOutlet with different objects

I was wondering how is it possible that use ONE IBOutlet to several objects , for example I have IBOutlet UITextView *myText; 我想知道如何对一个对象使用一个IBOutlet,例如我有IBOutlet UITextView *myText; then 3 UItextView on one view , so I want connect all of them with myText ! 然后在一个视图上有3个UItextView,所以我想将它们全部与myText连接!

You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet ). 您可以拥有一个声明为IBOutletCollection NSArray (而不是IBOutlet )。 In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags. 为了知道要从数组中移出哪个文本字段,可以在IB中的每个字段上设置一个标签,然后从数组中提取与特定标签匹配的文本字段。

Don't think so. 不要这样 Each UITextView in the one view would be a separate instance of the UITextView class. 一个视图中的每个UITextView都是UITextView类的单独实例。 The myText variable would only be able to point to one instance's memory address at a time. myText变量一次只能指向一个实例的内存地址。

I'm not sure what you're attempting to accomplish but you might be able to 'fake it' by having all the UITextViews use the controller as their delegate and setting their properties equal to each other when ever changes are detected. 我不确定您要完成什么,但是您可以通过让所有UITextView使用控制器作为其委托并在检测到任何更改时将其属性设置为彼此相等来“伪造”它。 (ie Every time the controller receives a message about the text property changing it sets all the UITextView objects text properties equal to the changed UITextView's text property). (即,每次控制器收到有关文本属性更改的消息时,它将设置所有UITextView对象的文本属性等于更改后的UITextView的文本属性)。

您可以使用1个对象并具有不同的标签。

This is inappropriate. 这是不合适的。 However, it's appropriate to hook one IBAction with multiple IBOutlets. 但是,将一个IBAction与多个IBOutlet挂钩是合适的。 So that when different buttons are pressed, they go to the same IBAction. 这样,当按下不同的按钮时,它们将进入相同的IBAction。 And the IBAction can tell where on earth that triggering is coming from by looking at the (id)sender argument. 通过查看(id)sender参数,IBAction可以判断触发是在哪里。

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

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