subclassing uiView > unrecognized selector - ios

I am trying to subclass UIView as BottomNavBar (code below).
I get the following error
'-[UIView setCenterImage:]: unrecognized selector sent to instance 0x14dc5050'
setting a couple of breakpoints the error comes from this line
self.centerImage=[UIImage imageNamed:#"icon_OK.png"]; //I used it just for testing it should be as follows:
self.centerImage=centerIcon;
BottomNavBar.m
- (id)initWithFrame:(CGRect)frame centerIcon:(UIImage*)centerIcon withFrame:(CGRect)centerFrame
{
self = [super initWithFrame:frame];
if (self) {
self=[[UIView alloc]initWithFrame:frame];
[self setBackgroundColor:UA_NAV_BAR_COLOR];
//--------------------------------------------------
self.centerImage=[UIImage imageNamed:#"icon_OK.png"]; //for testing
//self.centerImage=centerIcon; >>this is the one that should be used
self.centerImageView=[[UIImageView alloc]initWithFrame:centerFrame];
self.centerImageView.image=self.centerImage;
[self addSubview:self.centerImageView];
}
return self;
}
cropPhoto.m (where I'm trying to show an object of that class...)
CGRect bottomBarFrame = CGRectMake(0, self.view.frame.size.height-UA_BOTTOM_BAR_HEIGHT, self.view.frame.size.width, UA_BOTTOM_BAR_HEIGHT);
NSLog(#"UA_icon_ok: %#", UA_ICON_OK);
self.bottomNavBar = [[BottomNavBar alloc] initWithFrame:bottomBarFrame centerIcon:UA_ICON_OK withFrame:CGRectMake(0, 0, 90, 45)];
[self.view addSubview:self.bottomNavBar];
can anybody help on what's going wrong here?

self=[[UIView alloc]initWithFrame:frame];
Remove this line. Your view is already initialised, now you're replacing the value of self with a plain UIView.
I'm surprised you're not seeing a compiler warning here.

Related

awakeFromNib vs. initWithFrame for custome UITableViewCell

I need to make a custom UITableViewCell, so I created a subclass with associated nib file.
I need to add some subviews to it. Does awakeFromNib replace initWithFrame when using a nib file? I am using [[NSBundle mainBundle] loadNibNamed: in my cellForRowAtIndexPath method.
Do I use initWithFrame like so?
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 320)]
[self addSubview:self.horizontalTableView];
}
return self;
}
Or do I do it in awakeFromNib like so
- (void)awakeFromNib
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 320)]
[self addSubview:self.horizontalTableView];
}
When you are creating a Xib, initWithFrame will not be used. However, initWithCoder will be used. So, you can do extra instantiation in there. However, there is nothing wrong with initializing other controls in awakeFromNib either.
Edit: in response to your first comment below, check out this related question:
Should I be using awakeFromNib or initWithCoder here?

No visible #interface, adding parameter to initWithFrame

I have a ViewController (VCViewController) with this code in it:
touchView = [[VCTouchView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
in the UIView (VCTouchView) though I have:
(id)initWithFrame:(CGRect)frame andTheCameraController:(VCViewController*)cameraController
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Basically I have a ViewController which I want to add an overlay. On this overlay will be a button for the user to press that will take a photo. Quite simple requirement, yet I cannot understand why it can't see the parameter called andTheCameraController?
Any help greatly appreciated.
Mike
Have you added the new method to your VCTouchView.h file?
-(id)initWithFrame:(CGRect)frame andTheCameraController:(VCViewController *)cameraController;

Using SharedSingleton to call method

I have a UIviewController. I would to like to add as subview a UIView, but since the UIView doesn't have a viewdidload method, I need to manually call a method. I am doing this using shared singleton in this way:
In the view.m i have:
+ (TableLoadGroupMembersViewController *)sharedManager
{
static TableLoadGroupMembersViewController *shaderManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shaderManager = [[TableLoadGroupMembersViewController alloc] init];
});
return shaderManager;
}
- (void)LoadTheView
{
TableViewGroupMembers = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
TableViewGroupMembers.dataSource = self;
TableViewGroupMembers.delegate = self;
[TableViewGroupMembers addSubview:TableViewGroupMembers];
}
in the view.h I have
+ (TableLoadGroupMembersViewController *) sharedManager;
- (void)LoadTheView;
and in the viewcontroller.m I have:
CGRect frame = CGRectMake(100, 100, 300, 100);
TableLoadGroupMembersViewController *view = [[TableLoadGroupMembersViewController alloc] initWithFrame:frame];
[self.view addSubview:view];
TableLoadGroupMembersViewController* sharedSingleton = [TableLoadGroupMembersViewController sharedManager];
[sharedSingleton LoadTheView];
but the app crashes. Why?
EDIT:
The error is: 0x281db5d: pushl %esi ... Thread 1: EXC_BAD_ACCESS
but the app crashes. Why?
So, here you're creating a new instance of your view controller:
TableLoadGroupMembersViewController *view = [[TableLoadGroupMembersViewController alloc] initWithFrame:frame];
This will crash. Views have -initWithFrame methods, but your view controller probably doesn't.
Next, you're adding that view controller as a subview of self.view:
[self.view addSubview:view];
But a view controller is not a view, even if if you name the variable view, so you can't add it as a subview. If the first line didn't crash, this one will.
It looks like you're confusing your view controller class with a UIView subclass. Take a deep breath, read through the code while pretending that someone else wrote it, and try to figure out what's supposed to be happening here.
(It'd be a big help in the future, however, if you took the time to step through your code in the debugger and identify the specific line that's crashing.)
I'm not convinced (at all) that you need a singleton here for any reason, but the crash is almost certainly a matter of sending messages that the receiver doesn't implement, and it comes down to this confusing between view and view controller.

NSArrayM insertObject:atIndex:]: object cannot be nil'

After a lot of search, and tries with some solutions on stackoverflow, i didn't find any answer which could solve my error :
I have a UIViewcontroller, who's name is WorldViewController. In this UIViewcontroller, i had init some UIViews. I want to modificate some variables which depends of WorldViewController from some UIViews. And now that i put those lines :
WorldViewController * World = [[WorldViewController alloc] init];
it gives me :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
So, how to fix it ? Could it comes from the fact that WorldViewController is running ? And so, how to fix it ?
Edit : init method of my viewcontroller
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
World1 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
[World1 setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:World1];
[World1 release];
[self.view addSubview:ViewPig];
[self.view addSubview:level1view];
[self.view addSubview:level2view];
[self.view addSubview:LoadView];
[LoadView AnimPigLoadingWith:PigChargement];
[LoadView AppearLabelLoading];
[self.view addSubview:FondPauseGrise];
[self.view addSubview:FondPause];
[self.view addSubview:BoutonResume];
[self.view addSubview:BoutonHome];
[self performSelector:#selector(Chargement) withObject:nil afterDelay:0.5];
[self performSelector:#selector(ViewAppears) withObject:nil afterDelay:5.5+2.5];
[self performSelector:#selector(LabelPrevientDepartWithImage:) withObject:Trois afterDelay:9];
[self performSelector:#selector(LabelPrevientDepartWithImage:) withObject:Deux afterDelay:10];
[self performSelector:#selector(LabelPrevientDepartWithImage:) withObject:Un afterDelay:11];
[self performSelector:#selector(LabelPrevientDepartWithImage:) withObject:Go afterDelay:12];
[self performSelector:#selector(Lancement) withObject:nil afterDelay:(3)];
[self performSelector:#selector(GestionMaps) withObject:nil afterDelay:(11+2)];
}
return self;
}
Thanks !
I had a similar problem. My mistake was in not marking the view that I was trying to load as the initial view controller. My guess is that the there was no initial view controller to add to the array and hence the object was nil. I'm not sure how good this is at answering your question but it worked for me.
You haven't listed enough code in your question to definitely show where you're making an error.
Look in your code for any lines that say "insertObject:atIndex:". The object you're inserting is apparently nil.
If you can't find this line, add a symbolic breakpoint on the symbol "[NSArray insertObject:atIndex:]" (click the link to see specific instructions in an answer of a closely related question to yours) and see if you can break on it right before the crash happens.

Accessing self.view in UIViewControllers init method causes crash

I have a UIViewController with a custom init method that looks like this:
- (id)initWithFrame:(CGRect)frame_ customObject:(CustomObject *)object_ {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self = [super initWithNibName:#"ArtistViewController_iPad" bundle:nil];
} else {
self = [super initWithNibName:#"ArtistViewController" bundle:nil];
}
[self.view setFrame:frame_];
self.customObject = object_;
return self;
}
But when [self.view setFrame:frame_]; is called, it crashes with this in the log:
(null)
libc++abi.dylib: terminate called throwing an exception
This is how I allocate the UIViewController from another UIViewController:
CGRect frame = self.view.frame;
artistViewController = [[ArtistViewController alloc] initWithFrame:frame customObject:anObject];
the frame exists. self exists from [super initWithNibName:bundle];
But self.view seems to not exist. The nib files exist and have their view outlet hooked up.
Why does this happen?
You should not access the view from within the initializer. There are several more appropriate places to do this work, depending on what you really want to accomplish.
Read the view controller lifecycle to understand where you may want to place your modifications.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW1

Resources