Variable (IBOutlet UITextView) that works in some methods but not others
I have a view controller with this variable:
IBOutlet UITextView* chat
It works in some methods of the class but not for example in this method "chat" functions like I think it should:
// We are being asked to display a chat message
- (void)displayChatMessage:(NSString*)message fromUser:(NSString*)userName ofType:(NSString*)type withKey:(NSString*)key {
[chat appendTextAfterLinebreak:[NSString stringWithFormat:@"%@: %@", userName, message]];
[chat scrollToBottom];
NSLog(@"stuff to us %@: %@ type: %@", userName, message, type);
}
Others like this one it doesn't:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == input) {
// processs input
[chatRoom broadcastChatMessage:input.text fromUser:@"me" ofType:@"message"];
NSLog(@"Broadcast Chat Room is: %@", chatRoom);
[chat appendTextAfterLinebreak:[NSString stringWithFormat:@"me: %@", input.text]];
// clear input
[input setText:@""];
}
return YES;
}
When I use breakpoints, the variable chat shows 0x00 when it's not working.
Does anyone have any thoughts?
.h:
@interface FirstViewController : UIViewController <RoomDelegate>{
IBOutlet UILabel* label;
IBOutlet UIBarButtonItem *bbiOpenPopOver;
UIPopoverController *popOverController;
ServerListControll开发者_C百科er *serverListController;
Room* chatRoom;
IBOutlet UITextView* chat;
IBOutlet UITextField* input;
Room *newRoom;
}
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIBarButtonItem *bbiOpenPopOver;
@property (nonatomic, retain) UIPopoverController *popOverController;
@property (nonatomic, retain) ServerListController *serverListController;
@property(nonatomic,retain) Room* chatRoom;
@property (nonatomic, retain) UITextView* chat;
-(IBAction)togglePopOverController;
@end
I think I know what's going on.
In your FirstViewController
you are declaring the internal variable (ivar):
IBOutlet UITextView* chat;
Then, additionally, your are declaring the property:
@property (nonatomic, retain) UITextView* chat;
Unless you specify otherwise, each Objective-C property is supported by an auto-synthesized ivar whose name is the property name with the _
prefix before it (_chat
in this case).
So FirstViewController
has two different ivars for the same thing: chat
and _chat
. I'm guessing the methods that work use chat
, and the ones that fail use _chat
or self.chat
(which accesses _chat
internally). That, or the other way around.
My suggestion is that you get rid of all the ivars, and work only using the property accessors (self.propertyName
). That makes properties immediate and easily distinguishable from local variables and imho results in more readable and maintainable code.
Note that you can declare properties both in the .h
and in the .m
files. Properties declared in the .m
file will only be viewable from that file. I suggest you declare all the properties which are unneeded outside the class in the .m
file. This keeps the interface of your ViewController lean and clean.
I personally only access ivars directly when I have to overload or override property accessors or if doing that would effectively increase performance (which is not so common).
精彩评论