tags:

views:

619

answers:

2

Hi, I am a newbie in iphone developmenst its my second sample code. I am trying to add subViews to a View and generate events according to the view which is touched. 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 *redView;
    IBOutlet UIView *redView1;
    IBOutlet UIView *blueView;

}

//---expose the outlet as a property---
@property (nonatomic, retain) IBOutlet UIView *redView;
@property (nonatomic, retain) IBOutlet UIView *redView1;
@property (nonatomic, retain) IBOutlet UIView *blueView;

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

@end

And its .m file contains an action responder.what i am trying to do is when my views are touched they will generate an event which should be treated by this single method which will change the backcolor of the redview accordingly.

-(IBAction) viewClicked:(id) sender {
    redView.backgroundColor = [UIColor blackColor];
}

i worte the delegate in this way

    @interface testAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    testViewController *viewController;
}

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

@end

In testAppdelegate.m i am making views and subviews accoring to it and they are displaying it very well but i am unable to get any events on it when they are touched in (viewClicked:) method.How to do this????

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

    // Override point for customization after application launch
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor whiteColor];

    // Create a simple red square 
    CGRect redFrame = CGRectMake(0, 10, 320, 100);
    UIView *redView = [[UIView alloc] initWithFrame:redFrame];
    redView.backgroundColor = [UIColor redColor];
    //------------------------------------------------------------------------------------------------------- 
    // Create a simple blue square
    CGRect blueFrame = CGRectMake(5, 115, 100, 100); 
    UIView *blueView = [[UIView alloc] initWithFrame:blueFrame]; 
    blueView.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];


    // Add the square views to the window
    [window addSubview:redView];
    [window addSubview:blueView];
    [window addSubview:blueView1]; 

    [window addSubview:viewController.redView];
    [window addSubview:viewController.blueView];
        [window addSubview:viewController.blueView1];

    [window makeKeyAndVisible];
}

Thanks

A: 

How are you linking viewClicked to the views? Generally, to handle touches, you use touchesBegan:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)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");
    }

}
mahboudz
hi thx mahboudz for ur answer but i have updated my question hope it is now easy to understand.
rishi
Each of your views should have a touchesBegan: Then the touchesBegan method of the view that got touched would get called, not the viewController. If you want the viewController to do all the work then you need a hitTest:withEvent:.
mahboudz
A: 

yes i am using (- (void)touchesBegan:) method for getting handle touches.But My main problem is that i am getting this event on all view touches view and his subviews.I have sub-views that are attached to my parent view and i have to create different controller instances like

 @interface testAppDelegate : NSObject <UIApplicationDelegate>

{ UIWindow *window;

testViewController *viewController; //for main view

testViewController *viewController1;//for subview1

testViewController *viewController2;//for subview2

testViewController *viewController3;//for subview3

}

so that i can handle each view event separately with separate controller.When i click a subview it should detect which type of object it is and respond to that controller so that its color could change.how do i instantiate three different controllers and assing my views to it so that they can repond on their touch.

Plz help me

rishi