从一个View Controller传递字符串并在ios中的UITextView中显示它

时间:2014-10-17 05:51:44

标签: ios objective-c

我已经尝试过所有可以解决问题的方法,因为我是新手我没有得到明确的答案。 我有两个视图控制器。我将textview的数据传递给第二个控制器并在UILabel中显示它并没有问题。但现在我必须再将标签文本发送到同一文本视图进行编辑。问题是,当我双击标签时,它会导航到第一个视图,但不会在textview中显示文本。

-(void)AddTextToCanvasWithGesture:(UITextView*)takenText
{

    CGRect rect = CGRectMake(35, 60,takenText.contentSize.width,takenText.contentSize.height);

    UIView *holderView = [[UIView alloc] initWithFrame:rect];

    rect.origin.x=0;
    rect.origin.y=0;
    UILabel *textLabel=[[UILabel alloc]initWithFrame:rect];


    [textLabel setText:takenText.text];
     self.text=takenText.text;

    [textLabel setTextColor:takenText.textColor];
    [textLabel setFont:[UIFont fontWithName:takenText.font.fontName size:takenText.font.pointSize]];
    [textLabel setNumberOfLines:0];
    [textLabel sizeToFit];
    [holderView setTag:1];
    [textLabel setTag:1+1];
    [holderView addSubview:textLabel];


    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];

    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setDelegate:self];

    [holderView addGestureRecognizer:tapRecognizer];



    tapRecognizer = nil;

    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LaunchText)];

    [doubleTapRecognizer setNumberOfTapsRequired:2];
    [doubleTapRecognizer setDelegate:self];


    [holderView addGestureRecognizer:doubleTapRecognizer];
    holderView.userInteractionEnabled=YES;


    [tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer]; 

}

我必须将此标签的文本发送到2ndview控制器的textview

-(void) LaunchText{

    [self update:self.text];
      [self performSegueWithIdentifier:@"textAdd" sender:self]; 
}

-(void)update:(NSString*)string{

    TextAddViewController *txt=[[TextAddViewController alloc]init];

    txt.usingText.text=string;
}

我在第二个视图中以

的形式获取此字符串
-(void)viewDidLoad
{
    [super viewDidLoad];

    ViewController *vm=[[ViewController alloc]init];
    self.usingText.text=vm.text;
}

2 个答案:

答案 0 :(得分:1)

SecondViewController中覆盖以下方法,并将UILabel值传递给FirstViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue is called");
    if ([[segue identifier] isEqualToString:@"CONTROLLER_SEGUE"])  {

        TextAddViewController *vc = [segue destinationViewController];
        [vc.usgingText setText:@"SOME TEXT"];
    }
}

答案 1 :(得分:0)

包含textView的ViewController在其视图中需要一行代码才能执行Load方法。 textView。 text =文本来自何处。 发布更多代码可以帮助您更好。