English 中文(简体)
目标-C、i-SDK基础
原标题:Objective-C, iPhone SDK basics

在大韩民国,这是一个不容置疑的问题,但除这里外,我别无处可问。

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.

我试图将案件单独列入一个方法,这是我拥有的:

www.un.org/Depts/DGACM/index_french.htm

#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

www.un.org/Depts/DGACM/index_french.htm

#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 switchMethod performed
} 

//auto-generated code here

@end

当然,这部法律是不正确的,但这只是我想要得到的蓝图。

问题:

  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?
最佳回答

我确实不提你的问题,但也许你应该利用这样一种方法,在前向和后向案件之间转变:

- (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.:如果你的唯一问题是你的开关的重复,如果你不想使用我的方法,我不理解为什么你不直接叫你开你的开关。 两项职能的方法......

问题回答

问题1:

只是向自己发出信息,以援引转换方法。

问题2:

NSNumber是一个包含数字价值的物体。 将NSNumber改为:

int myIntValue = [number intValue];

1. 将一只 in改为一只NSNumber

NSNumber* number = [NSNumber numberWithInt: myIntValue];

然而,在你的例子中,最好把目前定义为暗中。 因此,你的法典应当研究这样的问题:

@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];
} 

// 等等

@end

NB尽管我界定了现时的财产,但我没有在法典中使用,这是一分之分。 主要问题是,如果任何人决定使用科索沃信托公司来观察当前情况,他们就会获得关于变化的通知。 我确实应该写好像这样的文字。

switch([self current])...

以及

[self setCurrent: 6]...

等等





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签