如果在If If Statement中

时间:2013-11-01 17:11:06

标签: objective-c if-statement mfmessagecomposeview

我正在写一些代码,它会在我的应用程序中写一条短信,如下所示:

MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
[messageComposer setMessageComposeDelegate:self];
//    Check The Device Can Send Text Messages
if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]];
    [messageComposer setBody:messageBodyText];
    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
//        Need to add an alert view
    NSLog(@"TEXT ISNT WORKING");
}

}

所以我目前有一个if语句检查设备是否能够发送消息,但是如何在其中添加ANOTHER if语句?我基本上想根据视图中的切换位置决定消息体是什么,如:

如果保留切换:消息正文为A
如果Switch是正确的:消息正文是B

2 个答案:

答案 0 :(得分:1)

这真的是一个开关的主要例子,甚至是加里描述它的例子。:

if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]

    //yourSwitchIsRightSide should be bool value
    switch (yourRightSide) {
        case YES:
            [messageComposer setBody:yourRightMessageBodyText];
            break;
        case NO:
            [messageComposer setBody:yourLeftMessageBodyText];
            break;
    }

    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
    //        Need to add an alert view
}

除了提高可读性之外,开关/外壳也可以更好地扩展。如果加里决定他以后会想要一些额外的选择,否则会造成一些混乱。 (在这种情况下,BOOL应该替换为对枚举的检查)

switch (switchDirection) {
    case MFSwitchDirectionLeft:
        [messageComposer setBody:yourLeftMessageBodyText];
        break;
    case MFSwitchDirectionRight:
        [messageComposer setBody:yourRightMessageBodyText];
        break;
    case MFSwitchDirectionUp:
        [messageComposer setBody:yourUpMessageBodyText];
        break;
    case MFSwitchDirectionDown:
        [messageComposer setBody:yourDownMessageBodyText];
        break;
    default:
        break;
}

答案 1 :(得分:-1)

尝试给定代码。检查你的开关值是向左还是向右,我认为你的开关变量是你的开关在右侧。

MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
[messageComposer setMessageComposeDelegate:self];
//    Check The Device Can Send Text Messages
if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]

  //yourSwitchIsRightSide should be bool value
    if(yourSwitchIsRightSide){
        [messageComposer setBody:yourRightMessageBodyText];
    }
    else{
        [messageComposer setBody:yourLeftMessageBodyText];
    }

    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
//        Need to add an alert view
    NSLog(@"TEXT ISNT WORKING");
}
}