Loading xib crashes app - objective-c

Hello all I am having a issue with showing a xib file from the main file not sure why this is happening with some of the xib files and not others.
if(segment == #"1"){
Results1 *myView1 = [[Results1 alloc]initWithNibName:#"Results1" bundle:nil];
[self.view addSubview:myView1.view];
}else if(segment == #"2"){
Results2 *myView2 = [[Results2 alloc]initWithNibName:#"Results2" bundle:nil];
[self.view addSubview:myView2.view];
}else if(segment ==#"3"){
Results3 *myView3 = [[Results3 alloc]initWithNibName:#"Results3" bundle:nil];
[self.view addSubview:myView3.view];
}else if(segment ==#"4"){
Results4 *myView4 = [[Results4 alloc]initWithNibName:#"Results4" bundle:nil];
[self.view addSubview:myView4.view];
}
Is my code the first xib files opens but not the rest I am not sure why, I have added .h files:
#import "Results1.h"
#import "Results2.h"
#import "Results3.h"
#import "Results4.h"
the app ends up on this line when debugging:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([TestTypingToolAppDelegate class]));
}
and then it crashes, anyone have any idea?

Tim, here is a picture from the Interface Builder, maybe it helps you:

You need to look at the console output when the application takes you to that line in your main.m. For me it was always a mistake on my part with the most common mistake being 1) The file does not exist (Results1.xib) or 2) I forgot to set the view outlet. As I mentioned the details from the exception in console output will help you.

Keep in mind that sometimes loading a view crashes on the device but not the simulator. In this situation, check to ensure that when you call:
initWithNibName:#"YourNibName";
that the casing of your nib name string is exactly the same as the nib file. The simulator ignores cases, but the device requires an exact match (pretty irritating)

Related

Xcode 4: Loading a Nib in a method in a different file

sorry if this question is elementary, but I've been stuck on this bug for the past 2 days & I haven't been able to get past it. I'm using Xcode 4.3.2
I'm trying to load a nib named AController.xib in a method called "- (void) process" in the file named BController.m
To be clear, I copied ./A/AController.xib (which is a UIView), ./A/AController.m, ./A/AController.h to the directory ./B
I only mention this because I'm not sure if it matters for my question.
Currently, my flow works as flows (which could be my problem):
A view loads with a "buy" button
the user clicks the "buy" button which has an IBOutlet named "buyNow"
"buyNow" calls "buy", which then calls "process"
process then tries to load the nib with the following (option 1):
AController *blah;
for (id object in bundle){
if ([object isKindOfClass:[AController class]])
blah = (AController *) object;
}
assert(blah != nil && "blah can't be nil");
[self.view addSubView: blah];
The error I get here is "Thread 1: signal SIGABRT" in main.m
I've also tried (option 2),
AController *myView = [[AController alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myView];
[AController release];
And (option 3)
AController * vc = [[AController alloc] initWithNibBundle:#"AController" bundle:nil]; [self.nc pushViewController:vc animated:NO];
I get the same error for all 3 choices. Each option was tried in the method "process". "process" is written in B.m. Can anyone offer some help so that I may figure this out? Any guidance as to why these options failed would be very helpful for my understanding and would be much appreciated. Thanks for helping a noob!
If AController is a UIView subclass, it cannot load a NIB. Verify it is in fact a controller, but from the initWithFrame and the way you are adding it to a view, it looks like it is not, or is being handled incorrectly.

Content not being added to UIPopoverController?

I'm new to iOS development so please cut me some slack.
I'm trying to learn how to use UIPopoverCotroller and I'm having a problem.
The popover appears correctly but nothing is added to it, its just black.
I'm using this line of code to try to add content to the popover:
self.photosPopover = [[UIPopoverController alloc]initWithContentViewController:self.photosPopoverViewController];
Heres some more code to help:
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
self.photosPopover=nil;
}
-(IBAction)photosPopoverButtonPressed:(id)sender{
if([self.photosPopover isPopoverVisible]){
[self.photosPopover dismissPopoverAnimated:YES];
self.photosPopover=nil;
return;
}
self.photosPopoverViewController = [[PhotosPopoverViewController alloc]init];
self.photosPopover = [[UIPopoverController alloc]initWithContentViewController:self.photosPopoverViewController];
[self.photosPopover setDelegate:self];
[self.photosPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[self.photosPopover setPopoverContentSize:CGSizeMake(320, 480)];
}
I'm using Xcode 4.3, ARC, and storyboards.
Thanks for your help!
-Shredder2794
You need to instantiate your view from storyboards. Sorry I didn't see this sooner...
self.photosPopoverViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"photosViewController"];
This is how you connect a customViewController to a viewController object in Storyboards. You build your PhotosPopoverViewController.h and PhotosPopoverViewController.m then you click your viewController object in storyboards and find the Custom Class identifier. Start typing your class name and it will autofill it.

Storyboard to a nib

I have an RSS parser in a UITableView that pushes to the detail view when one of the rows is selected. I had it working fine in the old UI format of using Nib files, but I wanted to transfer everything to UIStoryboard. I learned that UIStoryboards and nibs were compatible, so I decided I would keep the same code, but put the UITableView in the UIStoryboard and have the detail view be its own nib. I linked everything up and it should be working, and its not giving me any errors, but its not. Is there anything that I missed, or are storyboards and nibs not compatible at all.
Edit:
- (id)initWithItem:(NSDictionary *)theItem {
if (self == [super initWithNibName:#"RssDetailController" bundle:nil]) {
self.item = theItem;
self.title = [item objectForKey:#"title"];
}
return self;
}
Are you pushing from a UIViewController in UIStoryboard to a NIB file?
If so check out this sample project that pushes from storyboard to a NIB:
// in a navigation controller
// to load/push to new controller use this code
// this will be next screen
DetailsViewController *detailsViewController = [[DetailsViewController alloc ]init];
[self.navigationController pushViewController:detailsViewController animated:YES];
// to go back or pop controller use this
// now it will send back to parent screen
[self.navigationController popViewControllerAnimated:YES];

iPad Popover -[UIPopoverController initWithContentViewController: must not be called with `nil`

I'm still working my way around the iOS SDK and I have another probably easy one for you.
I'm getting the following error when attempting to present a popover:
CoreAnimation: ignoring exception: -[UIPopoverController initWithContentViewController: must not be called with nil.
I thought I had put in code to deal with this, although apparently not. Anyway, code is below. Any thoughts on this would be great. Cheers!
if(popoverController == nil)
{
NSLog(#"is nil");
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverDownload];
}
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(0,0,400,200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
UPDATE
I guess I'm not initialising popoverDownload correctly/at all.
In my .h file
PopoverDownloadViewController *popoverDownload;
#property (nonatomic,retain) PopoverDownloadViewController *popoverDownload;
UPDATE WITH ANSWER
And it was as easy as...
PopoverDownloadViewController *popoverDownload = [[PopoverDownloadViewController alloc] init];
Just to mark this answer closed. I needed to initialise the popover using the following code...
PopoverDownloadViewController *popoverDownload = [[PopoverDownloadViewController alloc] init];
Thanks to omz for the hints in the right direction.

How is an AppDelegate instanciated?

I have an iOS application for which I want to create a ViewController programmatically.
I started an empty XCode project and modified the main method so it looks like this
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"MyAppDelegate_iPad");
[pool release];
return retVal;
}
The app is a Universal Application, MyAppDelegate_iPad is a subclass of MyAppDelegate, which is a subclass of NSObject <UIApplicationDelegate>.
My problem is that the applicationDidFinishLoading method I've overridden in MyAppDelegate_iPad is never called (break point on the first line never hits). The method looks like this
-(void) applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if(!window)
{
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
rootController = [[MyViewController alloc] init];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
[window layoutSubviews];
}
I removed the line to link to a nib file from my plist file (I used to get the default "My Universal app on iPad" white screen) and now all that is displayed is a black screen. applicationDidFinishLoading is still not being called.
Am I doing something wrong? How should I properly create my AppDelegate instance?
There’s a main nib file that bootstraps your application. This nib file is referenced in the Info.plist file under the NSMainNibFile key and should contain an object that corresponds to your application delegate class (setting the Class attribute in Interface Builder). This application delegate object is referenced by the delegate outlet on the file’s owner placeholder.
So if I understand things correctly, the application loader loads the main nib file, setting itself as the nib owner. Its delegate property gets set to a fresh instance of your application delegate class, and so the loader knows where to dispatch the various application lifecycle event callbacks.
There’s an awesome blog post about Cocoa application startup on Cocoa with Love.
If you are making universal you don't need two different app delegate classes. see this link (my answer), it may be help you to make universal app.