我试着按照这个教程: http://www.youtube.com/watch?v=L-FK1TrpUng&feature=related 00:39:00左右。
Theres一个计数器,可识别每个切换到第二个视图控制器并对其进行计数。 应在第二个视图控制器上显示视图开关的数量。 但它不起作用,那就是代码: Test2ViewController.h
@interface Test2ViewController : UIViewController <UITextFieldDelegate> {
UINavigationController *navController;
Test21ViewController *test21View;
NSString *text;
IBOutlet UITextField *textField1;
}
@property(nonatomic, retain) UITextField *textField1;
@property (nonatomic, retain) UINavigationController *navController;
@property (copy) NSString *text;
-(IBAction)pushViewController:(id)sender;
@end
Test2ViewController.m:
#import“Test2ViewController.h” #import“Test21ViewController.h”
@implementation Test2ViewController
@synthesize textField1, navController, text;
-(IBAction)pushViewController:(id)sender {
static int count = 1;
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
[self.navigationController pushViewController:test21ViewController animated:YES];
test21ViewController.label = [NSString stringWithFormat:@"Pushed %d", count];
count++;
}
Test21ViewController.h:
@interface Test21ViewController : UIViewController {
UINavigationController *navController;
NSString *label;
IBOutlet UILabel *textLabel;
UITextField *tF1;
}
@property (copy) NSString *label;
@property (nonatomic, retain) UINavigationController *navController;
@property(nonatomic,retain)UITextField *tF1;
-(IBAction)feldeingabe:(id)Sender;
@end
Test21ViewController.m:
@implementation Test21ViewController
@synthesize label;
- (void)viewDidLoad {
textLabel.text = label;
}
任何想法为什么它不起作用? 提前致谢
第二个问题的代码:
static int count = 1;
Test2ViewController *test2ViewController;
test2ViewController.label = [NSString stringWithFormat:@"Pushed %d !!!!!", count];
[[self navigationController] popViewControllerAnimated:NO];
count++;
当前代码:
#import "Test21ViewController.h"
@implementation Test21ViewController
@synthesize navController, text, parentView;
-(IBAction)pushViewController2:(id)sender {
Test2ViewController *test2ViewController;
static int count = 1;
test2ViewController.parentView = self;
test2ViewController.label = [NSString stringWithFormat:@"Pushed %d !!!!!", count];
[self.navigationController pushViewController:test2ViewController animated:YES]; count++;
}
答案 0 :(得分:0)
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
test21ViewController.label = [NSString stringWithFormat:@"Pushed %d", count];
[self.navigationController pushViewController:test21ViewController animated:YES];
我交换了最后两行。可能是您在调用viewDidLoad之后设置了标签,因此没有在标签中设置值。
也就是说,最好将字符串作为init参数传递,而不是作为属性传递,然后在viewDidLoad中设置。
编辑:
要将其设置为init参数,您需要创建一个新的init方法来接受该参数。
- (id) initWithString:(NSString *)labelParam {
self = [super init];
if (self) {
this.label = labelParam;
}
return self;
}
答案 1 :(得分:0)
对于第二个问题(将字符串从子节点传递给父节点);
Test2ViewController.h
@interface Test2ViewController : UIViewController <UITextFieldDelegate> {
NSString *receiverPropertyFromChild;
}
@property(nonatomic, retain) NSString *receiverPropertyFromChild;
Test2ViewController.m:
-(IBAction)pushViewController:(id)sender {
static int count = 1;
Test21ViewController *test21ViewController = [[Test21ViewController alloc] init];
test21ViewController.label = [NSString stringWithFormat:@"Pushed %d", count];
test21ViewController.parentView = self;
[self.navigationController pushViewController:test21ViewController animated:YES];
count++;
}
Test21ViewController.h:
@class Test2ViewController;
@interface Test21ViewController : UIViewController {
Test2ViewController *parentView;
}
@property (nonatomic, assign) Test2ViewController *parentView;
Test21ViewController.m
-(IBAction)goToTest2ViewController:(id)sender {
parentView.receiverPropertyFromChild = @"the text you want to pass";
[self.navigationController popViewControllerAnimated:YES];
count++;
}