开发者

Objective-C, iPhone SDK basics

开发者 https://www.devze.com 2023-01-10 01:24 出处:网络
OK, this is going to be a stupid question but anyway I have nowhere to ask it except here. I have two buttons and there must be a switch-case statement performed on tapping any of them.

OK, this is going to be a stupid question but anyway I have nowhere to ask it except here.

I have two buttons and there must be a switch-case statement performed on tapping any of them. Of course I can put this statement in each IBAction code block but this code would look terribly.

I tried to put swith-case into a separate method and this is what I have:

.h file

#import <UIKit/UIKit.h>

@interface AppViewController : UIViewController
{
    IBOutlet UILabel *someTextLabel;
    NSNumber *current;
}

@property (nonatomic, retain) IBOutlet UILabel *someTextLabel;
@property (nonatomic, retain) NSNumber *current;

- (void) switchMethod;
- (IBAction) pressButtonForward;
- (IBAction) pressButtonBack;

@end

.m file

#import "AppViewController.h"

@implementation AppViewController

@synthesize someTextLabel;
@synthesize current;

current = 0;

- (void) switchMethod:current
{
    switch((int)current) {   

        case 0:
        //do something
        break;

        case 1 :
        //do something
        break;

        //etc

        default:
        //do something
        break;
    }
}

- (IBAction) pressButtonBack 
{ 
    if((int)current == 0) {
        current = 6; 
    }
    else {
        current--;
    }
    //here must be a switchMethod performed
}

- (IBAction) pressButtonForward 
{
    if((int)current == 6) {
        current = 0;
    }
    else {
        current++;
    }
    //here must be a switchMe开发者_如何学Pythonthod performed
} 

//auto-generated code here

@end

Of course this code is incorrect but this is just like a blueprint of what I wanted to get.

Questions:

  1. What is a correct way of using such switch-case statement as a separate method so that I could call it from IBAction methods?
  2. How should I cast data types for this code to work, or would it be better to use integer type (for "current" variable) everywhere?


I haven't really got your question but maybe you should switch between forward and backward cases using a method like this :

- (IBAction)pressButton:(id) sender;

and depending on the value of sender, you can maybe switch case between Forward And Backward. Thus, your code will be maybe more readable and you won't have to duplicate your switch.

N.B. : If your only problem is the duplication of your switch and if you don't want to use my method, I don't understand why you don't call directly your switchMethod in the two functions...


Answer to question 1:

just send a message to a self to invoke the switch method.

Answer to question 2:

NSNumber is an object that wraps a numeric value. To convert an NSNumber to an int:

int myIntValue = [number intValue];

To convert an int to a NSNumber

NSNumber* number = [NSNumber numberWithInt: myIntValue];

However, in your example, it's better to just define current as an int. So your code should look something like this:

@interface AppViewController : UIViewController
{
    IBOutlet UILabel *someTextLabel;
    unsigned int current;
}

@property (nonatomic, retain) IBOutlet UILabel *someTextLabel;
@property (nonatomic, assign) unsigned int current; // nonatomic might be redundant for POD types 

- (void) switchMethod;
- (IBAction) pressButtonForward;
- (IBAction) pressButtonBack;

@end

@implementation AppViewController

@synthesize someTextLabel;
@synthesize current;

//current = 0;  this wouldn't compile, in any case it is redundant, ivars start out as 0.

- (void) switchMethod
{
    switch(current) 
    {   
        // do switchy stuff
    }
}

- (IBAction) pressButtonBack 
{ 
    if(current == 0) 
    {
        current = 6; 
    }
    else 
    {
        current--;
    }
    [self switchMethod];
}

- (IBAction) pressButtonForward 
{
    if(current == 6) 
    {
        current = 0;
    }
    else 
    {
        current++;
    }
    [self switchMethod];
} 

// etc

@end

NB although I defined a property for current, I haven't used it in the code, which is a bit of a no-no. The main problem is that if anybody decides to observe current using KVO, they won't be notified of the changes. I should really have written things like

switch([self current])...

and

[self setCurrent: 6]...

etc

0

精彩评论

暂无评论...
验证码 换一张
取 消