开发者

How to implement multiple UIView classes in single UIViewController

开发者 https://www.devze.com 2023-01-12 11:54 出处:网络
In my viewbased application i loaded oneview as mainview and another view as subview of mainview. Its work well the code snippet is ,

In my viewbased application i loaded oneview as mainview and another view as subview of mainview. Its work well the code snippet is ,

In mainviewcontroller,

IBOutlet UIView *subView;
@property(nonatomic,retain) UIView *subView;
@synthesize subView;
[subView release];

//add subview
[self.view addSubview:subView];

//removefromsu开发者_如何学JAVAbview
[subView removeFromSuperview];

This code works fine.....

I dont want to create subview in mainviewcontroller, so i created a new UIView class and its named as subView, now i deleted all declarations of subView from mainviewcontroller and just import subView class in mainviewcontroller. And using this [self.view addSubview:subView];

This things not work great. Can anyone help me ... How can i interact a separate UIView class with UIViewcontroller.One more thing is that UIView class have labels and textboxes can i set values from UIViewController to UIView labels and textboxes ......

Is it possible ?

Thanks in advance.......Sorry for my bad english


You have a sub-class called Subview which is declared as a UIView, i.e.

@interface Subview : UIView {
    UILabel *foo;
}

@property (nonatomic, retain) UILabel *foo;

@end

Now you want to use this sub-class inside of your main UIView, which you had from the start. There are a few things you need to do.

  1. #import the Subview in your header file, and add an instance of it to your class.

#import "Subview.h"

and inside of your @interface's {}'s,

Subview *mySubview;

In the viewDidLoad class for your main view controller, around the bottom, add something like:

mySubview = [[Subview alloc] init];

[self.view addSubview:mySubview];

[mySubview release];

First line will allocate a new "Subview" for you, second line will add this to your view so you get the stuff it has, and third line will release it. It's okay to release it here, because "self.view" will now be responsible for it, so it won't vanish.

Lastly you need to set the view up in the init method for Subview. In Subview.m, do something like:

- (id)init
{
    if (self = [super init]) {
        foo = [[UILabel alloc] init];
        foo.text = @"Hello!";
        [self addSubview:foo];
    }
    return self;
}

And I think that should take care of it. You also want to release foo in -dealloc for Subview but you probably know how to do that stuff already.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号