Objective-c – How to attach more than one controller to views its subviews

iphoneobjective c

plz help me out
I am a newbie in iphone development its my second sample code. I am trying to add sub-views to a View and generate events according to the view/subview which is touched.i have added subviews in main view and all are showing all at one time. The projects I am experimenting with is a newly created, clean Window-Base application.

I wrote the following code into the one and only viewController's code:

@interface testViewController : UIViewController {

IBOutlet UIView *blueView1;
IBOutlet UIView *blueView2;
       :                        :
IBOutlet UIView *blueView6;

}

//—expose the outlet as a property—

@property (nonatomic, retain) IBOutlet UIView *blueView1;

@property (nonatomic, retain) IBOutlet UIView *blueView2;

      :                      :      *blueView6;

//—declaring the action—
-(IBAction) viewClicked: (id) sender;

@end

And its .m file contains (loadView method in which i am attaching subviews to it. They are displaying very well all at one time.what i am trying to do is when my view/subview are touched they will generate an event which should be treated by this single method according to their object which will change the back color of the view/subview accordingly.

- (void)loadView {

self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];

self.view.backgroundColor = [UIColor greenColor];

// Create a simple blue square

CGRect blueFrame0 = CGRectMake(5, 115, 100, 100);

UIView *blueView0 = [[UIView alloc] initWithFrame:blueFrame0];

blueView0.backgroundColor = [UIColor blueColor];

// Create a simple blue square

CGRect blueFrame1 = CGRectMake(110, 115, 100, 100);

UIView *blueView1 = [[UIView alloc] initWithFrame:blueFrame1];

blueView1.backgroundColor = [UIColor blueColor];

// Create a simple blue square

CGRect blueFrame2 = CGRectMake(215, 115, 100, 100);

UIView *blueView2 = [[UIView alloc] initWithFrame:blueFrame2];

blueView2.backgroundColor = [UIColor blueColor];

//———————————————————————————————————–

// Create a simple blue square

CGRect blueFrame3 = CGRectMake(5, 220, 100, 100);

UIView *blueView3 = [[UIView alloc] initWithFrame:blueFrame3];

blueView3.backgroundColor = [UIColor blueColor];

// Create a simple blue square

CGRect blueFrame4 = CGRectMake(110, 220, 100, 100);

UIView *blueView4 = [[UIView alloc] initWithFrame:blueFrame4];

blueView4.backgroundColor = [UIColor blueColor];

// Create a simple blue square

CGRect blueFrame5 = CGRectMake(215, 220, 100, 100);

UIView *blueView5 = [[UIView alloc] initWithFrame:blueFrame5];

blueView5.backgroundColor = [UIColor blueColor];

[self.view addSubview:blueView0];

[self.view addSubview:blueView1];

[self.view addSubview:blueView2];

[self.view addSubview:blueView3];

[self.view addSubview:blueView4];

[self.view addSubview:blueView5];

[blueView0 release];

[blueView1 release];

[blueView2 release];

[blueView3 release];

[blueView4 release];

[blueView5 release];

[self.view release];

}
-(IBAction) viewClickedid) sender {

{view/subview}.backgroundColor = [UIColor blackColor];

}

i worte the delegate in this way

@interface testAppDelegate : NSObject <UIApplicationDelegate>

{

UIWindow *window;

testViewController *viewController; //for main view

testViewController *viewController1;//for subview1

testViewController *viewController2;//for subview2

testViewController *viewController3;//for subview3

**upto 6 controllers

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet testViewController viewController;

@property (nonatomic, retain) IBOutlet testViewController viewController1;

**upto 6 controllers

@end

In testAppdelegate.m i am making a controller for main view but i dont know how to attach other controllers to other subviews.right now when i touch anywhere on the view the main view color changes.
how can i uniquely identify different subviews touch and make their color change??How to do this????

- (void)applicationDidFinishLaunching:(UIApplication *)application {    


// Override point for customization after application launch

checkAppController *rootController = [checkAppController alloc];


[window addSubview:[rootController view]];

[window makeKeyAndVisible];

}

i am getting all the touches events in this method

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event {

UITouch* touch = [touches anyObject];

NSUInteger numTaps = [touch tapCount];

if ([touches count] > 1)

NSLog(@"mult-touches %d", [touches count]);

if (numTaps < 2) {

}
else {

NSLog(@"double tap");

}

}

Best Answer

It is to do with the way you are structuring your application. For the BlueView1..6 to be handled by a separate view controller, it is that view controller which needs to create the view.

You need to create the view controllers for each blue view inside the main view controller, and then create each blue view inside the blue view inside each blue view controller.

Related Topic