Bonjour,
Je suis débutant et j'essaye des petites choses pour apprendre.
J'ai créé une vue qui devrait s'afficher juste au dessus du clavier virtuel (permettant d'enlever le clavier en appuyant sur le bouton de la vue).
Mais elle ne s'affiche que lorsque j'annule du texte en secouant l'iPhone. (Je ne sais même pas comment j'ai réussi à trouver ça)
J'ai Xcode 4.2, et je n'ai jamais testé les autres versions. Et je voulais savoir si c'est normal de ne pas avoir de Dealloc, et sinon dois-je en ajouter.
ViewController.h
ViewController.m
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #import <UIKit/UIKit.h> @class AccessoryTextViewController; @interface ViewController : UIViewController{ UITextView *TextView; AccessoryTextViewController *accessoryController; } @property (retain, nonatomic) IBOutlet UITextView *TextView; @end
AccessoryTextViewController.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 #import "ViewController.h" #import "AccessoryTextViewController.h" @implementation ViewController @synthesize TextView; - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; if(self.TextView) [self.TextView becomeFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:Nil]; } - (void)keyboardWillShow:(NSNotification *)n{ if (!accessoryController) { NSLog(@"accesoryController is not load."); accessoryController = [[AccessoryTextViewController alloc] init]; } self.TextView.inputAccessoryView = accessoryController.view; accessoryController.TextView = self.TextView; } - (void)viewDidUnload { [super viewDidUnload]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc removeObserver:self]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.TextView = Nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end
AccessoryTextViewController.m
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 #import <UIKit/UIKit.h> @interface AccessoryTextViewController : UIViewController{ UITextView *TextView; } @property (retain, nonatomic) UITextView *TextView; - (IBAction)TouchHide; @end
Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 #import "AccessoryTextViewController.h" @implementation AccessoryTextViewController @synthesize TextView; - (IBAction)TouchHide{ if (TextView) { [TextView resignFirstResponder]; } } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.TextView = Nil; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
P.S. Rajout du message d'erreur, lorsque que la vue est chargé.
"accesoryController is not load." c'est juste le message d'erreur que j'affiche.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 2011-10-16 16:34:44.830 TextEditor[4422:f803] accesoryController is not load. 2011-10-16 16:34:44.866 TextEditor[4422:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "AccessoryTextViewController" nib but the view outlet was not set.' *** First throw call stack: (0x13bb052 0x154cd0a 0x1363a78 0x13639e9 0xd7f0d 0xd83a9 0xd85cb 0x2ab0 0x9c6a39 0x1386885 0x13867a8 0x90b1aa 0x3a2afb 0x39df0a 0x39fb1f 0x39fba9 0x39fbf3 0x39a005 0xfecc8 0xfe77e 0x126299 0x28b6 0xd9fbf 0xda2d4 0xdb8c9 0xda730 0x235ea 0x138f9ce 0x1326670 0x12f24f6 0x12f1db4 0x12f1ccb 0x12a4879 0x12a493e 0x12a9b 0x2568 0x24c5 0x1) terminate called throwing an exceptionCurrent language: auto; currently objective-c (gdb)
Quand je modifie est lie la view a file's owner l'erreur ne s'affiche plus, mais il faut que je secoue l'iPhone pour la voir.
Je comprends que je dois lier le bouton à ma fonction, mais je ne comprends pas pourquoi je dois faire cela pour ma vue.
Partager