【发布时间】:2013-12-20 12:05:30
【问题描述】:
谁能帮我解决以下问题?
我是 Xcode 的新手,我还在学习它,但我可以弄清楚如何在 ViewController 之间传递数据。
这是我的工作代码:
FirstViewController.h:
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
- (IBAction)displayView:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lblFirst;
@end
FirstViewController.m:
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize lblFirst;
SecondViewController *secondViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.forwarded_lblFirst = lblFirst;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)displayView:(id)sender {
[self.view addSubview:secondViewController.view];
}
@end
SecondViewController.h:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic, retain) UILabel *forwarded_lblFirst;
@property (weak, nonatomic) IBOutlet UITextField *txtSecond;
- (IBAction)btnReturn:(id)sender;
- (IBAction)txtSecond_DidEndOnExit:(id)sender;
- (IBAction)btnSecond:(id)sender;
@end
SecondViewController.m:
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize forwarded_lblFirst;
@synthesize txtSecond;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnReturn:(id)sender {
[self.view removeFromSuperview];
}
- (IBAction)txtSecond_DidEndOnExit:(id)sender {
forwarded_lblFirst.text = txtSecond.text;
[txtSecond resignFirstResponder];
[self.view removeFromSuperview];
}
- (IBAction)btnSecond:(id)sender {
forwarded_lblFirst.text = txtSecond.text;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[txtSecond resignFirstResponder];
}
@end
这段代码运行良好,我在 SecondViewController 的文本框中输入的文本(在按下 btnSecond 或只是键盘上的 Return 键之后)显示为 FirstViewController 上的标签文本。
我的问题是,当我做完全相同的事情,但使用选项卡栏应用程序时,第一个选项卡上的标签不会改变。
我可以使用完全相同的代码(除了更改视图部分,因为我处理带有编程按钮的第一个应用程序,但对于 Tab-Bar-App 已经有按钮),还有两个 ViewControllers 用于标签栏应用程序也是如此(每个标签一个)。 所以代码是相同的,逐行,逐个字符,文件也是相同的,对于两个应用程序(基于视图,标签栏)。
为什么我的代码不起作用?我怎么解决这个问题? 谢谢!
【问题讨论】:
-
您的 firstviewcontroller 和 secondviewcontroller 是标签栏的不同标签,还是您在显示时从第一个视图调用 secondview?
-
我是这样使用的:link
标签: ios objective-c tabbar