не может следовать этому учебнику - PullRequest
0 голосов
/ 17 марта 2011

Я пытался следовать этому уроку: http://www.youtube.com/watch?v=L-FK1TrpUng&feature=related около 00: 39: 00.

Здесь есть счетчик, который распознает каждое переключение на второй контроллер вида и считает его.Количество переключателей вида должно отображаться на втором контроллере вида.Но это не работает, вот код: 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++;

}

Ответы [ 2 ]

0 голосов
/ 17 марта 2011

Для второй задачи (передача строки от потомка к родителю);

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++;    
}
0 голосов
/ 17 марта 2011

можете попробовать:

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...