【问题标题】:Add an inherited subview to my view将继承的子视图添加到我的视图
【发布时间】:2011-01-20 14:00:50
【问题描述】:


我目前正在从事一个具有很多相似性和很多观点的项目。 我会知道是否有办法将子视图从根类发送到继承类。

这是我的 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 中时,我可以很好地检测到我的滑动,但我还没有设法将我的自定义subView(来自我的 RootViewController)添加到我的 HomeViewController。

非常感谢。 问候,
kl94

【问题讨论】:

  • 与什么相似?你的问题不清楚。您能否让我们知道您尝试了什么以及它是如何不起作用的?您是什么意思将子视图从根类“发送”到继承类?
  • 我同意,我的问题不清楚。抱歉...我已经编辑了我的帖子,希望你能理解我的需要。

标签: iphone objective-c ipad inheritance view


【解决方案1】:

您应该向您的自定义视图添加一个构造方法,该方法使用您模型中的对象初始化您的视图。

例如:

我是你的 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];

希望这会有所帮助, 文森特。

【讨论】:

  • 管理我的自定义类是个好主意。你帮我找到了答案。这实际上是一个关于初始化的问题。
【解决方案2】:

好吧,我找到了解决方案。我的问题是我试图添加一个未初始化的 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 对象。我认为这是更好的方法。无论如何谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多