2012-08-27 103 views
0

我有一個ViewController,我已經設置了兩個子視圖。我想在它們之間切換。但是我的StepOne視圖中的按鈕沒有觸發。UIView(ViewControllers視圖子視圖)UIButton無法接收點擊事件

我是否以正確的方式使用視圖?我怎樣才能讓這個按鈕接收點擊事件?

由於

@interface UsageAlertsViewController() 

@end 

@implementation UsageAlertsViewController 

@synthesize currViewNum, stepOneView, stepTwoView; 

int padSides = 15; 
int summaryViewNum =99, stepOneViewNum = 1, stepTwoViewNum = 2; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self initAlerts]; 
} 

-(void) initAlerts 
{ 
    [self initViews]; 
    [self setCurrentView]; 
    [self gotoView]; 
} 

-(void) initViews 
{ 
    stepOneView = [[UIView alloc] init]; 
    stepTwoView = [[UIView alloc] init]; 
    stepOneView.userInteractionEnabled = NO; 
    stepTwoView.userInteractionEnabled = NO; 
    [self.view addSubview: stepOneView]; 
    [self.view addSubview: stepTwoView]; 
} 

-(void) showSummary 
{ 
    NSLog(@"showing summary"); 
} 

-(void) showStepOne { 
    NSLog(@"showing step one"); 

    //Intro label 
    int introLabelWidth = 200; 
    int introLabelHeight = 30; 
    int introLabelXPos = [GeneralHelper GetCenteredXPosForObj:introLabelWidth :self.view]; 
    int introLabelYPox = [GeneralHelper TopPadding] + 50; 
    UILabel *introLabel = [[UILabel alloc] initWithFrame:CGRectMake(introLabelXPos, introLabelYPox, introLabelWidth, introLabelHeight)]; 
    introLabel.text = @"Get alerts on the go!"; 
    introLabel.backgroundColor = [UIColor clearColor]; 
    introLabel.font = [UIFont boldSystemFontOfSize:20]; 
    introLabel.textColor = [[Global get] orangeClr]; 
    [self.stepOneView addSubview:introLabel]; 

    //Intro text 
    int introTVWidth = self.view.frame.size.width - (2*padSides); 
    int introTVHeight = 1; 
    int introTVXPos = [GeneralHelper GetCenteredXPosForObj:introTVWidth :self.view]; 
    int introTVYPox = 115; 
    UITextView* introTextView = [[UITextView alloc] initWithFrame:CGRectMake(introTVXPos, introTVYPox, introTVWidth, introTVHeight)]; 
    introTextView.backgroundColor = [UIColor clearColor]; 
    introTextView.font = [UIFont systemFontOfSize:15]; 
    introTextView.textAlignment = UITextAlignmentCenter; 
    NSString* tx = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"alertsIntroStep1" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; 
    introTextView.text = tx; 
    introTextView.editable = false; 
    introTextView.scrollEnabled = false; 
    [self.stepOneView addSubview:introTextView]; 
    [GeneralHelper resizeTextView:introTextView]; 

    //Intro button 
    int nextButtonWidth = 75; 
    int nextButtonHeight = 35; 
    int nextButtonXPos = [GeneralHelper GetCenteredXPosForObj:nextButtonWidth :self.view]; 
    int nextButtonYPos = 340; 
    UIButton *next = [OrangeButton Create:@"Yes!" :CGRectMake(nextButtonXPos, nextButtonYPos, nextButtonWidth, nextButtonHeight)]; 
    [next addTarget:self action:@selector(finishStepOne) forControlEvents:UIControlEventTouchUpInside]; 
    [self.stepOneView addSubview:next]; 
} 

-(void) finishStepOne 
{ 
    //Do whatever... 
    NSLog(@"TRY FINISH STEP ONE"); 
    currViewNum = stepTwoViewNum; 
    [self gotoView]; 
} 

-(void) showStepTwo { 

    //Intro text 
    int introTVWidth = self.view.frame.size.width - (2*padSides); 
    int introTVHeight = 1; 
    int introTVXPos = [GeneralHelper GetCenteredXPosForObj:introTVWidth :self.view]; 
    int introTVYPox = 115; 
    NSLog(@"showing step two"); 
    UITextView* introTextView = [[UITextView alloc] initWithFrame:CGRectMake(introTVXPos, introTVYPox, introTVWidth, introTVHeight)]; 
    introTextView.backgroundColor = [UIColor clearColor]; 
    introTextView.font = [UIFont systemFontOfSize:15]; 
    introTextView.textAlignment = UITextAlignmentCenter; 
    NSString* tx = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"alertsIntroStep2" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; 
    introTextView.text = tx; 
    introTextView.editable = false; 
    introTextView.scrollEnabled = false; 
    [self.stepTwoView addSubview:introTextView]; 
    [GeneralHelper resizeTextView:introTextView]; 
} 

-(void) gotoView 
{ 
    printf("SHOWING %u", currViewNum); 

    if(currViewNum == summaryViewNum) { 
     [self showSummary]; 
    } 
    if(currViewNum == stepOneViewNum) { 
     [self.view bringSubviewToFront:stepOneView]; 
     [self showStepOne]; 
    } 
    if(currViewNum == stepTwoViewNum) { 
     //[self.view bringSubviewToFront:stepTwoView]; 
     [self showStepTwo]; 
    } 
} 

-(void) setCurrentView 
{ 
    bool isActive = false; //TODO, (temp) this represents if alerts are active or not 
    if(isActive) { 
     currViewNum = 99; 
    } else { 
     currViewNum = 1; 
    } 
} 

回答

1

U均具有intially在你的initView方法制成stepOneViewstepTwoView'suserInteractionEnabled = NO

因此button裏面stepOneViewnotrecievetouch任何方式。所以它not receivingclickevents

+0

這是不工作之前,我把它放在,我把這個,因爲有人提到它可能會修復的東西...... – Baconbeastnz