2014-05-09 37 views
1

我正在開發一個sencha Touch應用程序併爲ios構建使用phonegap。 我想在ios鍵盤上顯示一個數字工具欄,並在textField中獲取數字按鈕動作。 我可以在鍵盤上顯示工具欄,但無法根據按鈕tap/clik更新textFild值。這裏是我的代碼。請幫我解決這個問題。我在MainViewController.m中完成了代碼。我不熟悉目標c。Sencha Touch,Phonegap,IOS,鍵盤頂部工具欄

#import "MainViewController.h" 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowNotification:) name:UIKeyboardDidShowNotification object:nil]; 
    } 
    return self; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark 
- View lifecycle - (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
} 

- (void) keyboardDidShowNotification:(NSNotification *)aNotification { 
    UIBarButtonItem *my0Button = [[UIBarButtonItem alloc] initWithTitle:@"0" style:UIBarButtonItemStyleBordered target:self action:@selector(addNumberToString:)];  
    UIBarButtonItem *my1Button = [[UIBarButtonItem alloc] initWithTitle:@"1" style:UIBarButtonItemStyleBordered target:self action:@selector(addNumberToString:)]; 
    // UIToolbar *keyboardToolbar=nil; 
    UIToolbar *extraRow = [[UIToolbar alloc] init]; 
    extraRow.barStyle = UIBarStyleDefault; 
    extraRow.translucent = YES; 
    extraRow.tintColor = [UIColor grayColor]; 
    [extraRow sizeToFit]; 
    NSArray *buttons = [NSArray arrayWithObjects: my0Button, my1Button, nil]; 
    [extraRow setItems:buttons animated:YES]; 
    NSArray *array = [[UIApplication sharedApplication] windows]; 
    for (UIWindow* wind in array) { 
     for (UIView* currView in wind.subviews) { 
      if ([[currView description] hasPrefix:@"<UIPeripheralHostView"]) { 
       for (UIView* perView in currView.subviews) { 
        if ([[perView description] hasPrefix:@"<UIWebFormAccessory"]) { 
         [currView addSubview:extraRow]; 
         [perView removeFromSuperview]; 
        } 
       }   
      }   
     }  
    } 
} 

-(void) addNumberToString: (id) sender  { 
    // HERE I WANT TO SHOW THE BUTTON TEXT TO TEXTFIELD, 
} 
@end 

回答

1

最後我得到了輸出。

UIButton *clickButton=sender; 
UILabel *label=clickButton.titleLabel; 
NSString *buttonText=label.text; 

NSString *javaScript = [NSString stringWithFormat:@"var textField = document.activeElement;" "textField.value = textField.value+'%@';" "document.activeElement.form.submit();", buttonText]; 
[webView stringByEvaluatingJavaScriptFromString:javaScript];