UIView(ViewControllers视图的子视图)UIButton无法接收点击事件

时间:2012-08-27 06:01:53

标签: iphone objective-c ios

我有一个ViewController,我在其中设置了两个子视图。我想在他们之间切换。但是我的StepOne视图中的按钮没有触发。

我是否以正确的方式使用观点?如何通过此按钮接收点击事件?

由于

@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 个答案:

答案 0 :(得分:1)

您最初使用stepOneView方法制作了stepTwoView'suserInteractionEnabled = NO initView

所以button stepOneView not recieve touch not receiving click。所以events {{1}} {{1}}