Reputation: 129
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(id)init{
self = [super initWithNibName:@"WritingView" bundle:nil ];
if (self) {
//self.view.delegate = self;
self.txtMain.userInteractionEnabled = YES;
self.txtMain.delegate = self;
}
return self;
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
return [self init];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"textViewShouldBeginEditing:");
return YES;
}
self.txtMain is my root view in xib:WritingView,and my view controller implemented protol UITextViewDelegate ,like this:
@interface WritingViewController : UIViewController<UITextViewDelegate>
{
}
@property (strong, nonatomic) IBOutlet UITextView *txtMain;
I've made breakpoint in textViewShouldBeginEditing or other func in UITextViewDelegate,but never come in why? by the way this ViewController was created by another ViewController ,like this:
WritingViewController *aViewController = [[WritingViewController alloc] initWithNibName:@"WritingView" bundle:nil];
[self.view.superview addSubview:aViewController.view];
[self.view removeFromSuperview];
can anybody tell me why it does not work,and then i changed the init code:
-(id)init{
self = [super initWithNibName:@"WritingView" bundle:nil ];
if (self) {
UITextView *txtView = [[UITextView alloc] initWithFrame:self.view.frame];
txtView.textColor = [UIColor blackColor];
txtView.font = [UIFont fontWithName:@"Arial" size:18.0];
txtView.text = @"Now is the time for all good developers tocome to serve their country.\n\nNow is the time for all good developers to cometo serve their country.";//
self.txtMain = txtView;
self.txtMain.userInteractionEnabled = YES;
self.txtMain.delegate = self;
[self.view addSubview:txtView];
}
return self;
}
obviously I used a blank View as root-view,but this time , when i click the text,programe creshed in main():
return UIApplicationMain(argc, argv, nil, NSStringFromClass([WheelDemoAppDelegate class]));
and in console:
2013-11-03 22:53:57.514 Wheel demo[1718:a0b] *** -[WritingViewController respondsToSelector:]: message sent to deallocated instance 0x9b4eeb0
(lldb)
Upvotes: 2
Views: 327
Reputation: 3406
You need to somehow make sure the ARC system is not removing your object from memory. ARC will remove objects as soon as their retain count reached zero, and the object is no longer in scope. The ARC system is very different from a garbage collector. You might want to read up on it: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
The error you are getting is the result of not retaining your view controller (WritingViewController
). Try creating a property on the class creating the viewcontroller:
@property (nonatomic,strong) WritingViewController *writtingVc;
And set it to your instance of WritingViewController right after you have created it.
Upvotes: 1
Reputation: 119031
Your first code doesn't work because when you call
self.txtMain.userInteractionEnabled = YES;
self.txtMain.delegate = self;
In your unit method the view hasn't been loaded yet. That means that your txtMain
doesn't exist.
To fix, put that code in viewDidLoad
instead of init
.
Upvotes: 0