2012-02-03 10 views
0

アクションメソッドを使用してテキストフィールドから入力したデータに対話するにはどうすればよいですか?ボタン? たとえば、ユーザーはテキストフィールド賃金に9を入力し、時間フィールドには10を入力し、ボタンを押すと賃金(9)が計算され、時間(10)で計算され、テキストフィールドpayCheck、90に印刷されます。Objective-Cボタンを使ってUITextFieldsからデータ(文字列、整数、倍精度など)を取得する方法

答えて

0
NSString *text = [UITextField text]; 

を得ることができますしたい、例えばint型の値、

int intValue = [text intValue]; 

は、だからあなたのボタンをクリック扱う機能では、これらの値を取得し、

[UITextField setText:text]; 
を使用して別のテキストフィールドに割り当て
0

文字列にはcomponentSeperated by functionを使用できます。あなたがタイプにあなたを変換次にプロパティ「テキスト」

でテキストフィールドから値を取得することができますので、あなたの場合には、あなたは9と10を得ることができ、その後、あなたはそれを掛けることができますし、90

0

3つのテキストボックスを配置することができます.1つは賃金、1つは時間単位、もう1つは出力支払いチェックです。ユーザが賃金および/または時間を入力すると、賃金は給与チェックテキストボックスに示される。ここではコード化された例です:

#import <UIKit/UIKit.h> 

@class TestViewController; 

@interface TestAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    TestViewController *viewController; 

    UITextField *txtWage; 
    UITextField *txtHours; 
    UITextView *txtPay; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet TestViewController *viewController; 

-(void)calculatePay; 

@end 

との.mファイル:

#import "TestAppDelegate.h" 
#import "TestViewController.h" 

@implementation TestAppDelegate 

@synthesize window; 
@synthesize viewController; 


#pragma mark - 
#pragma mark Application lifecycle 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch. 

    // wage 
    txtWage = [[[UITextField alloc] initWithFrame:CGRectMake(10.0, 10.0, 50.0, 25.0)] autorelease]; 
    txtWage.backgroundColor = [UIColor whiteColor]; 
    txtWage.placeholder = @"wage"; 
    [viewController.view addSubview:txtWage]; 

    // hours 
    txtHours = [[[UITextField alloc] initWithFrame:CGRectMake(70.0, 10.0, 50.0, 25.0)] autorelease]; 
    txtHours.backgroundColor = [UIColor whiteColor]; 
    txtHours.placeholder = @"hours"; 
    [viewController.view addSubview:txtHours]; 

    // payCheck 
    txtPay = [[[UITextView alloc] initWithFrame:CGRectMake(150.0, 10.0, 50.0, 25.0)] autorelease]; 
    txtPay.backgroundColor = [UIColor whiteColor]; 
    txtPay.editable = NO; 
    [viewController.view addSubview:txtPay]; 

    // list for changes to wage and hours 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(calculatePay) 
               name:UITextFieldTextDidChangeNotification 
               object:nil]; 

    // Add the view controller's view to the window and display. 
    [self.window addSubview:viewController.view]; 
    [self.window makeKeyAndVisible]; 

    // have keyboard show up in wage box 
    [txtWage becomeFirstResponder]; 

    return YES; 
} 

#pragma mark - 
#pragma mark Memory management 

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { 
    /* 
    Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. 
    */ 
} 


- (void)dealloc { 
    [txtWage release]; 
    [txtHours release]; 
    [txtPay release]; 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 

#pragma mark - 
#pragma mark Other Methods 

-(void)calculatePay { 
    NSLog(@"calulating pay"); 
    txtPay.text = @""; 
    if (txtWage.text.length > 0 && [txtWage.text intValue]>0 
     && txtHours.text.length > 0 && [txtHours.text intValue]>0 
     ) 
    { 
     int pay = [txtWage.text intValue] * [txtHours.text intValue]; 
     txtPay.text = [[NSNumber numberWithInt:pay] stringValue]; 
    } 
} 


@end 

ここ.hファイルです

関連する問題