0

我正在使用应用程序来锁定、解锁和打开我的汽车后备箱。唯一的问题是我不知道如何修改 Xcode 项目,所以有 3 个按钮。基本上现在如果我输入“U”然后输入-汽车解锁,“L”然后输入-汽车锁,然后输入“T”然后输入-后备箱打开。我想添加三个按钮来模拟这三件事,并一起消除打字。如果您想查看我的 adruino 或 xcode 项目代码,我可以上传它们。我在下面的文本框上放了一些代码。

    BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSString *text = textField.text;
    NSNumber *form = [NSNumber numberWithBool:NO];

    NSString *s;
    NSData *d;

    if (text.length > 16)
    s = [text substringToIndex:16];
     else
        s = text;

    d = [s dataUsingEncoding:NSUTF8StringEncoding];
    if (bleShield.activePeripheral.state == CBPeripheralStateConnected) {
    [bleShield write:d];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:text, TEXT_STR, form, FORM, nil];
    [tableData addObject:dict];
    [_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
    NSLog(@"%f", _tableView.contentOffset.y);
    [self.tableView reloadData];
}

textField.text = @"";

return YES;

谢谢您的帮助!

4

1 回答 1

0

Your view controller probably has a textFieldShouldReturn method which is taking the string value from the text field and building a parameter to a call that initiates sending the command. If not this method then perhaps its action method linked to the text field.

You'll need to duplicate parts of that code into a method that receives a string parameter instead of taking it from the text field, say named sendLockCommand:(NSString *)commandString (assuming you're coding in Objective-C, also like that repo).

Make action methods for your buttons, something like lockDoors, unlockDoors, openTrunk, in each call [self sendLockCommand:@"L"], each with the appropriate string. Wire up the buttons to those actions and you're good to go.

于 2015-11-13T07:39:34.687 回答