2

我有一个有多个视图的程序,其中一个有标签(这个问题的LabelView),另一个有文本字段(这个问题的TextView)。我想从文本字段中获取信息,并将其放在标签上,我认为最简单的方法是从 TextView 调用方法 LabelView。

我尝试了几件事:

  1. 我尝试在 LabelView 中放置一个使用参数更改标签值的方法。然后调用TextView中的方法,通过参数传递数据。

  2. 我试着用这个问题做一个代表。不幸的是,最后一步(添加settingView.viewControllerDelegate=selfviewDidLoad方法)失败了。settingView是一个未声明的标识符。

有谁知道我应该怎么做?

编辑:这是我使用 Xman 的父/子视图理念所做的一些事情。这是在 TextView.m 中:

NSInteger curScore = [self.ToP1Score.text integerValue];
NSInteger curPhase = [self.ToP1Phase.text integerValue];
self.ToP1Score.text = [NSString stringWithFormat:@"%d", (curScore += [self.player1Txt.text integerValue])];
if ([self.player1Txt.text integerValue] < 50) {
    self.ToP1Phase.text = [NSString stringWithFormat:@"%d", (curPhase += 1)];
}
4

2 回答 2

2

有两种方法可以实现这一点。

1)制作ChildView为childView,ParentView以便您可以直接访问以下ParentView属性ChildView

父视图.h

@interface ParentView : UIView
@property (nonatomic,strong) UITextField *mytextField;
@end

子视图.h

#import "ParentView.h"

@interface ChildView : ParentView
@property (nonatomic,strong) UILabel *myLabel;
@end

2) 使用通知

把它放在isFirstView的位置textField

[[NSNotificationCenter defaultCenter] postNotificationName:@"sendData" object:yourTextFiels userInfo:nil];

把它放在SecondView你所在的地方Label

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabelValue:) name:@"sendData" object:nil];

还有这个方法

-(void)changeLabelValue(NSNotification *)iRecognizer {

    UITextField *myTextField = iRecognizer object];

    //Here You can change the value of label
}

希望这会帮助你。

于 2013-09-04T05:09:29.343 回答
0

假设您将一些文本从 firststViewController 传递给 secondViewController,然后在 firststviewcontroller 中添加这些代码行

-(IBAction)goToNextScreen:(id)sender
{
  secondViewController *newVc = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil];
 // or if you are using storyboard
 // NewViewController *newVc = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
 newVc.text = @" Your text ";
[self.navigationController pushViewController:newVc animated:YES]; 
}

在 2ndViewController.h 文件中

#import <UIKit/UIKit.h>

@interface secondViewController : UIViewController
{
  NSString text;
}
@property(nonatomic, retain)NSString text;
@property (weak, nonatomic) IBOutlet UILabel *lbl;

@end

在 .m 文件中

#import "secondViewController.h"

@interface secondViewController ()

@end

@implementation secondViewController
@synthesize text,lbl;

- (void)viewDidLoad
{
   lbl.text = text;
}
@end

希望这会帮助你..不要忘记投票...

于 2013-09-04T05:31:16.947 回答