tags:

views:

47

answers:

1

Hi,

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];

//removefromsubview
[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

A: 

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.

Kalle
IBOutlet UIView *subView; @property(nonatomic,retain) UIView *subView; @synthesize subView; [subView release];In these methods , i am creating a subview as xib files in mainviewcontroller .so we dont need allocation and deallocation code just drag and drop the subview as uiview.Its work fine. Mainly why i want separte uiview subclasses means there is more number of labels and textboxes used in mainviewcontroller so i want to separte those variables from Mainviewcontroller,so i want to add as separted UIView subclasses.
dragon
You're confusing me when you say you want to create subclasses and then talking about subView which is NOT a subclass, just a regular old UIView. Let me see if I get you straight: you have a bunch of labels and textboxes, which you want to separate from your view controller, by adding as separate UIView subclasses...? That doesn't make sense. What is the reason for subclassing in the first place?
Kalle
sorry i can't explain what i exactly want is .. Thanks for your replay.
dragon
don't accept my answer if it's not an answer! :P if you try to explain what your problem is in a different way, maybe i or someone will understand what you mean. I think you are misusing the word 'subclass' -- subclassing means you create a new .m and .h file called, for example, "MyCookieView" which has something like @interface MyCookieView : UIView in it. Is this what you mean, or something else?
Kalle
yes you are correct. I created a new subclass Its name Subview it has two file Subview.h and Subview.m and also have @interface Subview : UIView . i.e@interface Subview : UIView { IBOutlet UILabel *someLabel; IBOutlet UITextField *someTextField;}@property(nonatomic,retain) IBOutlet UILabel *someLabel;@property(nonatomic,retain) UITextField *someTextField;@endI want interact this subclass with my mainviewcontroller.m file.For example#import "Mainviewcontroller.h"#import "SubView.h"Subview *view2;view2.someLabel.text = @"some string";
dragon
Is this above method is understandable kalle ?
dragon
Yeah, that makes more sense. Firstly, you need to get the grasp on class declaration versus object name. UIView *subView will not create an instance of your Subview class. Hang on, I'm gonna update the answer...
Kalle
All right, updated. Check that out and see how that works out for you.
Kalle
Thanks kalle for your quick replay
dragon