Iphone 将继承的子视图添加到“我的视图”
Iphone 将继承的子视图添加到“我的视图”,iphone,objective-c,ipad,inheritance,view,Iphone,Objective C,Ipad,Inheritance,View,我目前正在做一个项目,有很多相似之处,有很多观点。 我知道是否有办法将子视图从根类发送到继承类 这是我的RootViewController: @class CustomView; @interface RootViewController : UIViewController { IBOutlet CustomView *_aCustomView; } @end @implementation RootViewController - (void)viewDidLoad {
我目前正在做一个项目,有很多相似之处,有很多观点。 我知道是否有办法将子视图从根类发送到继承类 这是我的RootViewController:
@class CustomView;
@interface RootViewController : UIViewController {
IBOutlet CustomView *_aCustomView;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swipRecognizer;
// swipe left management
swipRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipeFrom:)];
swipRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
swipRecognizer.numberOfTouchesRequired = 2;
swipRecognizer.delegate = self;
[self.view addGestureRecognizer:swipRecognizer];
[swipRecognizer release];
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"swip detected");
[self.view addSubview:_aCustomView];
}
HomeViewController:
@interface HomeViewController : RootViewController {
}
@end
我会创建一些从我的RootViewController
继承的ViewController,就像这样HomeViewController
,以保存行代码。当我在我的HomeViewController
中时,我可以很好地检测到我的滑动,但我还没有将我的自定义子视图
(来自我的RootViewController)添加到我的HomeViewController中
非常感谢。
问候,kl94您应该向自定义视图添加一个构造函数方法,该方法使用模型中的对象初始化视图 例如: 在您的RootViewController中,您有一个Customer对象和一个CustomerView,显示有关此客户的信息 在CustomerView中,您应该创建一个方法:
-(id) initWithFrame:(CGRect) frame customer:(Customer) customer;
如果需要在HomeViewController中显示相同的CustomerView,只需添加一个Customer对象作为HomeViewController的属性,并在viewDidLoad中仅初始化CustomerView,方法如下:
CustomerView *customerView = [[CustomerView alloc] iniFrame:CGRectMake(...) customer:currentCustomer;
[self.view addSubView:customerView];
[customerView release];
希望这有帮助,
文森特。我找到了一个解决办法。我的问题是我试图添加一个未初始化的IBOutlet对象,希望以后不要后悔: 在我的HomeViewController中,我覆盖了方法
initWithNibName:bundle:
:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
和我的RootViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"RootViewController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
与什么相似?你的问题不清楚。您能告诉我们您尝试了什么,以及它是如何不起作用的吗?将子视图从根类“发送”到继承类是什么意思?我同意,我的问题不清楚。对不起。。。我编辑了我的文章,希望你们能理解我需要什么。这是一个好主意,可以管理我的自定义类。你帮我找到了答案。这实际上是一个关于初始化的问题。如果初始化是你的问题,你可以在你的控制器中钩住viewDidLoad。是的,现在我知道了。但目前我使用initWithNibName:bundle:来初始化一些IB对象。我认为这是更好的办法。反正是Thx