Objective c 在UIViewController中获取UIView属性值
我有一个CustomView(UIView的子类),其中包含一些奇怪的UIButtons,单击ViewController上的按钮时会显示这些按钮。如何在ViewController的CustomView中设置按钮的标记值 CustomView.m -我将设置按钮标签=005 ViewController.m -我怎么才能到这里 我浏览了下面的文章,但没有了解如何实现它。Objective c 在UIViewController中获取UIView属性值,objective-c,ios7,uiview,uiviewcontroller,delegates,Objective C,Ios7,Uiview,Uiviewcontroller,Delegates,我有一个CustomView(UIView的子类),其中包含一些奇怪的UIButtons,单击ViewController上的按钮时会显示这些按钮。如何在ViewController的CustomView中设置按钮的标记值 CustomView.m -我将设置按钮标签=005 ViewController.m -我怎么才能到这里 我浏览了下面的文章,但没有了解如何实现它。 如果您在ViewController中引用了CustomView,则可以执行以下操作: for (UIView *v in
如果您在ViewController中引用了CustomView,则可以执行以下操作:
for (UIView *v in self.customView.subviews) {
if ([v isKindOfClass:[UIButton class]]) {
int tag = v.tag;
}
}
但是,如果没有对自定义视图的任何引用,请尝试以下操作:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[CustomView class]]) {
CustomView customView = (CustomView*)v;
for (UIView *v1 in customView.subviews) {
if ([v1 isKindOfClass:[UIButton class]]) {
int tag = v.tag;
break; // You have the tag exit loop
}
}
break; // you have custom view exit loop
}
}
您不需要使用代理或任何东西。您只需要在ViewController中引用CustomView,然后使用以下内容:
for (UIView *view in [_myCustomView subviews]) {
if ([view isKindOfClass:([UIButton class])]) {
//Test the tag
if (view.tag == 5) {
//found it! do something with it.
break;
}
}
}
要在视图控制器中添加引用,请将其添加到.m文件的@interface部分
@property (strong, nonatomic) CustomView* myCustomView;
然后,在创建自定义viee时,将其分配给myCustomView,如下所示:
_myCustomView = [[CustomView alloc] init...];
你在ViewController中有任何对CustomView的引用吗?@Greg没有。我在ViewController.m中使用[[CustomView alloc]initWithFrame:]