質問

I have a weird issue with a UIStepper (and it's accompanying UITextField) Consider this code snippet:

@interface LTRPageTracker : UIView <UITextFieldDelegate>
{
     UIStepper* page_move;
     UITextField* page_no_view;
}
-(void) nextOrPrevPage:(id)sender forEvent:(UIControlEvents) event;

@implementation LTRPageTracker

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGRect stepperFrame, pageNoframe;
        pageNoframe.origin = frame.origin;
        pageNoframe.size.height = frame.size.height;
        pageNoframe.size.width = frame.size.width/2;
        stepperFrame.origin.x = pageNoframe.origin.x + pageNoframe.size.width +1;
        stepperFrame.origin.y = frame.origin.y;
        stepperFrame.size = pageNoframe.size;

        page_move = [[UIStepper alloc] initWithFrame:stepperFrame];
        [page_move setMinimumValue:0];
        [page_move setValue:7];
        page_move.maximumValue =1000;
        [page_move addTarget:self action:@selector(nextOrPrevPage:forEvent:) forControlEvents:UIControlEventValueChanged];

        page_no_view = [[UITextField alloc] initWithFrame:pageNoframe];
        page_no_view.delegate = self;
        page_no_view.backgroundColor = [UIColor redColor];
        [self addSubview:page_no_view];
        [self addSubview:page_move];
        [page_move sendActionsForControlEvents:UIControlEventValueChanged];
        [page_move setEnabled:YES];
    }
    return self;
}

-(void) nextOrPrevPage:(id) sender forEvent:(UIControlEvents) event {
    //assert(sender == page_move);
    NSLog(@"Event is %x", event);
    page_no_view.text = [[NSNumber numberWithDouble: page_move.value] stringValue];
}

I have added this View to the navigation bar. And I can decrement the value of the UIStepper but no increment it (the event will simply not get triggered for increment but will do so decrement). WHY? Using iOS 7, running on simulator.

役に立ちましたか?

解決

Whenever you have a problem with a UI element not responding, the first thing you should check is whether it (or part of it) is outside the bounds of its superview (the part that's out of the bounds will not respond). An easy way to do this, is to give the superview a background color. In the init method assign a background color to self, and see what that shows you. I'm betting that you'll see that the right side of the stepper is not within its superview, so you'll need to make that view bigger, or change the stepper's position within that view.

If you make the view's frame 150x30, and change these two lines in the view's init method, I think it should work ok for a number as large as 1000:

    pageNoframe.size.width = frame.size.width/4; // changed 2 to 4
    stepperFrame.origin.x = pageNoframe.origin.x + pageNoframe.size.width+20; // changed 1 to 20
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top