Search code examples
iosuistepper

UIStepper won't display


I'm basically having the same issue as this question. But guess what? That question doesn't have any good answer.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if([indexPath row] == 0){

        UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
        if( aCell == nil ) {
            aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
            aCell.textLabel.text = @"Colors";
            aCell.selectionStyle = UITableViewCellSelectionStyleNone;

            /*
            UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
            aCell.accessoryView = switchView;
            [switchView setOn:NO animated:NO];
            [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            */
            UIStepper *stepperView = [[UIStepper alloc] initWithFrame:CGRectZero];
            NSLog(@"The stepper is %@", stepperView);
            aCell.accessoryView = stepperView;
           [stepperView setMaximumValue:10];
           [stepperView setMinimumValue:0];
           [stepperView addTarget:self action:@selector(stepperChanged) forControlEvents:UIControlEventValueChanged];
           NSLog(@"The stepper is %@", stepperView);
       }
       return aCell;
    }

    UITableViewCell *aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];

    NSString *s = @"foo";
    s = [s stringByAppendingFormat:@"%d", [indexPath row]];
    aCell.textLabel.text = s;

return aCell;

}

So if I switch the code to the commented out code, that works just fine (displaying the UISwitch). So all I tried to do was replace the UISwitch with the UIStepper and it just doesn't show up. I hate objective-c.

Oh, and both of those printlines show the value is (null). Right after I create it, it's null?

EDIT: iOS 5.0. I feel like the answer might have to do with why this:

NSLog(@"%@", [[UISwitch alloc] init]);

is not null while

NSLog(@"%@", [[UIStepper alloc] init]);

is null.


Solution

  • The code compiled in iOS 5.0, but the simulator was for iPhone 4.3. So the iPhone didn't know what UIStepper is, but knew what UISwitch was. Why it didn't cause the thing to crash (which could have saved me a lot of time) is a mystery.