2009-09-10 2 views
0

나는 두 번째 샘플 코드 인 iphone developmenst에서 초보자입니다. 뷰에 하위 뷰를 추가하고 터치 한 뷰에 따라 이벤트를 생성하려고합니다. 내가 새로 만든 깨끗한 창 - 기본 응용 프로그램이 실험하고있는 프로젝트는 subviews 터치 이벤트를 얻고 그에 따라 적절한 조치를 위임하는 방법

나는 하나에 다음 코드를 썼습니다 만의 ViewController의 코드 :

@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 

하고하는 .m 파일이 동작 응답자를 포함 내가 무엇을하려고하는지는 내 견해를 건 드리면 그에 따라 redview의 배경색을 변경하는이 단일 메서드로 처리해야하는 이벤트를 생성합니다.

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

은 내가 testAppdelegate.mi에서

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

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

@end 

는 전망하고 여기에 accoring에 파단을 만드는 중이라서 그들은 매우 잘 표시되어있다하지만 난 그것에 이벤트를 얻을 수 없습니다 나는이 방법으로 대리자를 worte 그들이 (viewClicked :) 메서드에 닿았을 때. 이것을하는 방법 ????

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

감사

+0

: 일반적으로 접촉을 처리하기 위해, 당신은 touchesBegan를 사용합니다. – Jacob

답변

0

어떻게의 의견에 viewClicked를 연결하는? 당신은 아마 아이폰 친구들이 볼 수 있도록 아이폰 질문으로이 태그를 지정할

- (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"); 
    } 

} 
+0

안녕하세요 thx mahboudz에 대한 답변을하지만 난 내 질문을 업데이 트했습니다 이해하기 쉽게 지금은 바랍니다. –

+0

각각의 뷰에는 touches가 있어야합니다 .Began : 그러면 touches 뷰의 touchesBegan 메소드가 호출되고 viewController는 호출되지 않습니다. viewController가 모든 작업을 수행하게하려면 hitTest : withEvent :가 필요합니다. – mahboudz

관련 문제