addSubview到super.superView
问题描述:
mainView_a-> ScrollView_b-> ScrollView_c-> View_d->按钮
和按钮touchUpInside我需要addSubView到ScrollView_b 是possigle后????
我试过[super.super addSubview:myNewView];
-(IBAction)showPhotoGallery:(id) sender
{
UIScrollView *photoGalleryScV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 800)];
photoGalleryScV.backgroundColor = [UIColor redColor];
[super.super addSubview:photoGalleryScV];
//[photoGalleryScV release];
}
它不工作:(
答
super
是指你的类的父类,而不是上海华关键字
尝试
[self.superview.superview.superview addSubview:photoGalleryScV];
这样:
-
self
在Button -
self.superview
是view_d -
self.superview.superview
是scrollview_c -
self.superview.superview.superview
是scrollview_b
虽然这似乎有点脆弱,你可以使用应用程序委托的主视图或根控制器和工作下来比较好代替?
答
self
在该代码中通常不是按钮。 self
将是一个UIViewController。 sender
将是UIButton。因此
[sender.superview.superview.superview addSubview:photoGalleryScV];
可能工作。
无论您如何在视图结构中重新排列视图,都可以在视图控制器中定义一个指向ScrollView_b的出口。在界面构建器中进行连接(或者如果您愿意,可以在loadView中的代码中进行连接)。然后
[self.scrollView_b addSubview:photoGalleryScV];