使用else字符串中的字符串并将其显示在不同的视图中

时间:2014-04-12 17:02:31

标签: ios objective-c if-statement uiview

我需要帮助显示一个字符串,如果If-Else语句显示else,则在另一个View Controller中。我比较新,很抱歉,如果你觉得这很愚蠢。我在ViewControler.m文件中尝试过:

-(IBAction)ButtonTouched:(id)sender
{
    [self.InputField resignFirstResponder];
    if (NSOrderedSame == [self.InputField.text caseInsensitiveCompare:@"teacher"])
    {
        _Correct.hidden = FALSE;
        _Hider.hidden = TRUE;
        _FalseHider.hidden = FALSE;
        _SHOWCORRECT.hidden = TRUE;
        _CorrectAnswer.hidden = TRUE;
    } else {
        _FalseHider.hidden = TRUE;
        _Correct.hidden = TRUE;
        _SHOWCORRECT.hidden = FALSE;
        _CorrectAnswer.hidden = TRUE;

        NSString *ElseLabel = [NSString stringWithFormat:@"teacher"];
        self.OutputLabel.text = ElseLabel;
    }
}

Elselabel位于MainStoryboard中的另一个ViewControler中....当我通过模态segue去那里时,它不显示任何东西!感谢所有回复!

1 个答案:

答案 0 :(得分:0)

完成以下步骤

  1. 转到Appdelegate.h并添加一个像这样的NSString

    @property(nonatomic,retain)NSString * lableString;

  2. 转到Appdelete.m并合成字符串

    @synthesize lableString;

  3. 然后改变你的功能//不要忘记#import" AppDelegate.h"

    -(IBAction)ButtonTouched:(id)sender
    {
    
    [self.InputField resignFirstResponder];
    if (NSOrderedSame == [self.InputField.text caseInsensitiveCompare:@"teacher"])
    {
    _Correct.hidden = FALSE;
    _Hider.hidden = TRUE;
    _FalseHider.hidden = FALSE;
    _SHOWCORRECT.hidden = TRUE;
    _CorrectAnswer.hidden = TRUE;
    } else {
    _FalseHider.hidden = TRUE;
    _Correct.hidden = TRUE;
    _SHOWCORRECT.hidden = FALSE;
    _CorrectAnswer.hidden = TRUE;
    
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.lableString = [NSString stringWithFormat:@"teacher"];
    
    }
    }
    
  4. 并在您的不同视图中使用此

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    self.OutputLabel.text = appDelegate.lableString;
    
  5. 不是: - 这不是最佳方法,但它肯定会给你一个小课程