Crasher in a custom view as UINavigationControllers navigationItem.titleView - ios

I have an UIViewController with several subviews in its view property (UISearchbar and several UIButtons). The UIButtons hooked up to typical IBActions like -(IBAction)buttonPressed:(id)sender for the UIControlEventTouchUpInside state - it doesn't matter if I do it in IB or programmatically.
- (void)viewDidLoad {
MUZTitleViewController *title = [[MUZTitleViewController alloc]
initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
}
In my project there's also an UINavigationController. When I set the navigationItem.titleView of the UINavigationBar to the view of my UIViewControllers view I get an EXC_BAD_ACCESS exception, as soon as I tap one of the button. I don't know why this is.
I uploaded a small sample project to illustrate my problem: Test010.xcodeproj (it's ARC enabled)
More and more I come to the conclusion that it's not a good idea to use the UIViewControllers view and assign it to the titleView but I don't see any alternative here.
Edit: Sorry, the sample project commented out the call which causes the exception. I reuploaded the linked project file.
Edit^2: As PengOne pointed out I've skipped the exact error message I got:
2011-09-10 23:09:50.621 Test010[78639:f803] -[CALayer buttonPressed:]: unrecognized selector sent to instance 0x9254ae0
2011-09-10 23:09:50.623 Test010[78639:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer buttonPressed:]: unrecognized selector sent to instance 0x9254ae0'

Have you tried setting NSZombieEnabled to YES? If I do this, the console shows the following output:
2011-09-10 22:56:23.329 Test010[6481:ef03] *** -[MUZTitleViewController
performSelector:withObject:withObject:]: message sent to deallocated
instance 0x7a7ff70
As the project is ARC enabled, the controller seems to get deallocated some time after this line:
MUZTitleViewController *title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
I am not sure what the best solution is, but a property definitely helps to prevent the exception like so:
// MUZDetailViewController.h
#property (strong, nonatomic) MUZTitleViewController *title;
// MUZDetailViewController.m
#synthesize title;
self.title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;

The problem that you were having with ARC can also be resolved by setting the initial view controller of your application as your main window's rootViewController property instead of using addSubview.
Doing this avoids the need to add each custom view controller as a property.

Related

Crash in self.view after constructor on iOS 8.1 and 8.2

I'm trying to do a simple SpriteKit action - adding a view to a scene:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"LevelsEditorStoryboard" bundle:[NSBundle mainBundle]];
self.levelsEditor = [mainStoryboard instantiateViewControllerWithIdentifier:#"LevelsEditorId"];
[self.scene.view addSubview:self.levelsEditor.view];
On iOS 8.0 and 8.3 is working fine and on iOS 8.1 and 8.2 I'm getting the following error:
SPBingo[75304:80943666] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextSelectionView name]: unrecognized selector sent to instance
I realised that this crash caused by the UITextField.
Once I'm getting rid of it, replacing the UITextField with some UITextView ( not ideal but whatever) The view is loading but when I try to click on a UITextView it crashed. The other Input views work (switches, dataPicker, tableView etc.)
After a deep dive into it I figured out that the problem occur in the contractor already. When I put breakpoint on the the second line in the following code :
-(instancetype) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
}
return self;
}
and try to print "self.view" I'm getting the following error:
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3)..
The process has been returned to the state before expression evaluation.
ODD!
You cannot access the view until after the "viewDidLoad:" method signature is called, otherwise it attempts to create the view before all the prerequisites are in place.
Make sure to call self.view from "viewDidLoad:", "viewWillAppear:" or "viewDidAppear:" when it has properly been setup.
So.. it is an internal bug by Apple which create in 8.1 and fixed in 8.3.
The solution is avoid using the UITextView contractor "initWithFrame" and use the following one:
UITextView *textView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer];

addChildViewController giving NSException error?

I am new to using this method so I could be doing this completely wrong so here is my code:
#property (nonatomic, weak) ConverterViewController *converterViewController;
#property (nonatomic, weak) CalculatorViewController *calculatorViewController;
If I am understanding this code correctly, these are acting as references to Two different ViewControllers.
Then I have this in my viewDidAppear method:
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];
I am getting an NSException at the first line when I try and add it as a child view controller. So not knowing whether or not this should then call some methods in my ConverterViewController class I put some breakpoints within that class both the initWithNibName and viewDidLoad methods and I found that neither of these methods are being called, so Im not exactly sure what is wrong. Then again Im not really sure what could go wrong so any help is greatly appreciated.
This is all I get from the console:
libc++abi.dylib: terminating with uncaught exception of type NSException
Updated Answer:
[self addChildViewController:_converterViewController]; does not create the converterViewController.
It simply takes the converterViewController object and adds it as a childViewController to self.
You will need to allocate memory and instantiate the object converterViewController before -addChildViewController: or else it's value will be nil and nothing will happen.
So... something this:
_converterViewController = [[ConverterViewController alloc] initWithNibName:#"ConverterViewController"
bundle:nil];
//now... adding it as childViewController should work
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
//optional: give it a frame explicitly so you may arrange more childViewControllers
//[_converterViewController.view setFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:_converterViewController.view];

unrecognized selector sent to instance

I am trying to get a pop up menu on iPhone.
The main app is using storyboard, but the pop up is a separate xib file that I load:
menu = [[UIViewController alloc] initWithNibName:#"SimpleMenuController" bundle:nil];
[self.view addSubview:menu.view];
and I slide it in and out with animation when pressing a button.
that works fine, but I get a problem when I try to press a button inside that pop up menu
I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[UIViewController PressCategory:]:
unrecognized selector sent to instance 0x8d3c040'
I have connected the button to PressCategory function
I have connected the view to the file owner.
What I have noticed is that my ViewController is called SimpleMenuViewController, that is where the PressCategory function is, so of course it will not find the selector. But I don't know what I am doing wrong in connecting in the xib file.
Change your code to:
menu = [[SimpleMenuViewController alloc] initWithNibName:#"SimpleMenuController" bundle:nil];
so that you're instantiating the correct class.
Do you have PressCategory function in your SimpleMenuViewController?
If yes, then check weather its parameterized or not.
Declare function in .h file like this:
-(IBAction)PressCategory:(UIButton *)sender;
Define it in .m file like this:
-(IBAction)PressCategory:(UIButton *)sender {
// write your code here
}

iOS Private API performSelector crash

Basically I want to get a list of action targets for a UIButton. I have gone through this and my question is slightly different because I do not know what the target is. All I have is a UIButton object. So here's what I did to capture all action targets.
Inspired by below method which works where I get firstResponder object as valid pointer.
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:#selector(firstResponder)];
I used class-dump on UIKit to see UIWindow class and I found firstResponder as below.
NS_CLASS_AVAILABLE_IOS(2_0) #interface UIWindow : UIView {
#package
UIResponder *_firstResponder;
}
Then I checked UIControl which via class-dump as
NS_CLASS_AVAILABLE_IOS(2_0) #interface UIControl : UIView {
#package
NSMutableArray* _targetActions;
}
So here's what I try to do and it crashes.
NSMutableArray *arr = (NSMutableArray*)[((UIControl*)btn) performSelector:#selector(targetActions)];
NSLog(#"%#",arr);
Sounds like conspiracy against me. But more likely I am goofing up some thing. Does any know how to access targetActions Array of UIControl?
EDIT: Here's the error message -
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UIRoundedRectButton targetActions]: unrecognized selector sent to instance 0x1c0ab0'
Any help appreciated.
According to the UIControl documentation, the message to send to get a list of targets is allTargets not targetActions as you suggest. The rest of the solution is in the accepted answer to How to get UIButton Target, Action and Control events?
A debugging technique you can use when you don't know what you're doing is to use respondsToSelector to check whether you're sending a message that the object can respond to:
when to use respondsToSelector in objective-c

UIScrollView loses reference to delegate

I need to create a number of UIScrollViews dynamically and fill them with content. This is all good except when i set the delegate to self and pan the list i get this exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString scrollViewDidScroll:]: unrecognized selector sent to instance 0x7581230'
NSCFString obviously isn't my view controller (which implements the protocol UIScrollViewDelegate) so from what i gather somehow the memory gets messed up and it doesn't keep the reference correctly. Occasionally this can be something else too which strongly points to something being wrong with the memory
Here's the code to create the list:
for (NSUInteger i = 0; i < self.stories.currentStory.selectableWordCount; i++) {
UIScrollView *list = [[UIScrollView alloc] init];
list.alwaysBounceVertical = YES;
list.showsVerticalScrollIndicator = NO;
list.clipsToBounds = NO;
list.delegate = self;
list.pagingEnabled = YES;
[self.view addSubview:list];
.. // add UILabels to the list, set the frame, contentSize etc
[self.wordLists addObject:list]; // this is a #property (nonatomic, strong) NSMutableArray, declared in a private interface()
}
If i NSLog the delegate it's correct. respondsToSelector also matches fine. Interestingly if i comment out the scrollViewDidScroll: respondsToSelector: doesn't match any more and (probably because of this) the UIScrollView won't attempt to call this method any more. This then means that it can reach the delegate correctly to check for the method availability but when it gets called something goes wrong.
I'm targeting iOS5 with ARC. If this wasn't the case i would assume that i messed something up with the memory myself but now i don't have the same control.
I'm having a hard time debugging this issue, any help on how to proceed would be appreciated
D'uh. I was obviously looking in the wrong place. The view controller was added through a .xib and the view was pointing to a subview on the stage. However i needed to create an IBOutlet to the view controller in the main view controller to make sure it stays in memory. Hopefully this can help somebody else with a similar problem :)

Resources