Xcode4: How to create a menu for a game? - iphone

I have a game with 4 game modes. the problem is that I want to create a main menu to choose a mode, because instead of that menu there is a TabBar. I'm trying to do that putting each mode in different .xib files, and create an other .xib file for the menu.
Menu.m:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] init];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
with this I'm getting this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '- [UIViewController _loadViewFromNibNamed:bundle:] loaded the "ViewController" nib but the view outlet was not set.'
I checked that "view" is linked to "View" on IB so I don't know what to do...
I'm not an advanced developer, thanks for helping!

You should try to do the following:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
Seems like you forget to put initWithNibName inside the allocation of the view.
Also, make sure that your ViewController.xib is the correct name of the view controller and check if it has the correct outlets linked.
Check View Identity > Class Identity is set too. If not, type in the name of your controller, ViewController
EDIT
Remove everything in the xib, "Window" and "ViewController".
Drag an UIView on the left column and set its outlet.
Click on File's Owner and then, on the Inspector, Identity inspector section (third one from the left), type in the class of your view, `ViewController´.

Related

Issues Using .xib With Storyboard

Hey I'm trying to open a view controller that is designed using a xib interface file. I am using the following lines of code to generate the controller from within a view controller created as a storyboard component.
YourViewController *viewController [[YourViewControlleralloc] initWithNibName:#"ViewControllerName" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
I have never set up a view controller with a .xib before nor have I ever linked to one via storyboard so anything could be wrong. Here is the error I am getting when I try to present the view.
2013-05-17 13:06:45.120 Coffee[8991:907] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason:
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the
"PeekPagedScrollViewController" nib but the view outlet was not set.'
Set the File's Owner in the .xib to the class of your view controller (perhaps it is called PeekPagedScrollViewController).
Then connect its view outlet to the main view in the .xib file.
It was connected properly I had just forgotten to declare an initWithNibName method and therefore it crashed when I called it from storyboard. Silly mistake but thanks for all the great feedback #matt
Set the Storyboard view controller custom class to your class name (the one your xib is also referencing), then in that same class, simply override initWithCoder with:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [[super initWithCoder:aDecoder] initWithNibName:#"YourCustomViewControllerXibsName" bundle:nil];
if( self ) {
}
return self;
}
This will enable you to design layout and segue transitions in Storyboard, but design your layout in a standalone nib file.
Storyboard will handle instantiation, but when initWithCoder: is called, initWithNibName: will be the method that instantiates self via the xib.

ios converting ipad app to universal showing this error nib but the view outlet was not set

I have iPad app and it's working great. I'm trying to convert the app to universal app.
1.- In the summary of the app I change it from iPad to Universal
2.-I add it a nib for the iPhone iPhoneView.xib
3.-in my app delagate I add this code:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
}
else
{
self.viewController=[[ViewController alloc]initWithNibName:#"iPhoneView" bundle:nil];
}
4.-in the iPhone nib I add viewcontroller as custom class
I run the app using the simulator and I got this error:
[UIViewController _loadViewFromNibNamed:bundle:] loaded the "iPhoneView" nib but the view outlet was not set.'
I search for a way to fix and seems like I need the reference outlets to the nib but when I try to drag the connection from the "New Referencing Outlet" on the right side of xcode in the last tab to the nib I can not make the connection or if I try to drag the connection from the view icon to the files owner I can not make the connection neither. Any of you knows what I'm doing wrong?
I'll really appreciate you help.
go to the iPhoneView.xib first set file owner to your view controller
After that click on the file owner(yellow block ) and click on the right most arrow in following picture. set all the connections . This will solve your problem.(Probably i think your view is not set to view outlet)

class is not key value coding-compliant for the key navigationBar

I have added a UINavigationbar and UIscrollView to UIView(SecondView).When I click a button in firstView it should take me to secondView.
On button click :
SecondView *secondview=[[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
[self presentModalViewController: secondview animated:NO]; //error at this line
[secondview release];
In the secondView.h
#property(nonatomic,retain)IBOutlet UINavigationBar *navigationBar;
#property(nonatomic,retain)IBOutlet UIScrollView *testscroll;
SecondView.m:
#synthesize navigationBar,testscroll;
But Im getting error like :
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: SecondView setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key navigationBar.
My secondView.xib is like this :
Couldnot understand where Im going wrong?
This error generally comes when you have created an outlet in your xib and then by mistake (or knowingly) you have deleted that iboutlet object, or vice versa.
So check your xib's iboutlet connections carefully.
Check the spelling:
#property(nonatomic,retain)IBOutlet UINavigationBar *navigationBar;
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: SecondView setValue:forUndefinedKey:]: this class is not key
value coding-compliant for the key navigationbar.
Note the difference of navigationBar and navigationbar, it's case sensitive.
you can presenting ModelViweController With navigationbar like this way:-
SecondView *objSecondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:objSecondView];
// add navigation bar image at hear
UIImage *image = [UIImage imageNamed:#"nav_launcher.png"];
[navbar.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
navbar.navigationBar.tintColor=[UIColor whiteColor];
[self presentModalViewController:navbar animated:YES];
and you can Push One View to Another View like:-
SecondView *objSecondView =[[SecondView alloc]initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:objSecondView animated:YES];
NOTE
Some time its error occurs because of we are putting wrong Nib name at this line of code : initWithNibName:#"SecondView" bundle:nil];
UPDATE
no need to add navbar tin image at SecondViewcontroller you can add all stuff hear like barbuttonItem, tincolor , navigatin BackgroudnColor ect.
pushViewController: is used to push a UIViewController. It looks like SecondView is a UIView, not a UIViewController.
I solved it myself.
I was wrong with nothing.Everything was OK.
I have added a new view and made the same connections and got it.Don't know why this happens regularly in xcode.
If you face this problem again,
try to: select File's Owner then click on the "Connection Inspector" (upper on the top of right pane), you will see all the outlets. Look for any sign like this (!) you will find it on the small circle which indicates a missing outlet, all you have to do is linking it properly or remove the outlet.
For erroneous case Rakesh provided correct answer (outlet was deleted - need accurately check your xib's / storyboards) with small addition:
it might be also caused by renaming outlet by simply "Find&Replace". In such case the outlet is re-named everywhere in sources, but not in storyboard.
In such case make sure you accurately renamed the outlet in storyboards too, e.g.:
grep -r --include "*.storyboard" navigationBar .
Than everything looks to be ok: XCode caching your xib's, to fix the behavior:
clean build
remove app from device/simulator
restart the app
Restart simulator might be also necessary (for me never was required for device)
I think you have not connect some object to the File's Owner in that UIViewController's xib.
Check one time my friend.

Universal UIViewController

Please help me, I never made universal UIViewControllers with xib for iPhone/iPad. How I can create class with .m and .h files and _iphone.xib and _ipad.xib?
I was try to create xib manually and create outlets for view, but after pushing this controller I had uncaught error:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FullNewsViewController" nib but the view outlet was not set.
Thanks!
The problem, what I can see from the error you posted, is that you have not set the owner properties of the NIB correctly.
Your nib files does not seem to have the correct file's owner and thus can't set the view on the view controller.
Open the nib, and go to File's Owner in the left since of the screen. Set the Custom Class property to the class of your viewController.
The right click on the File's Owner again and drag the view property to your view.
You should follow Apple iOS App Programming Guide
Creating a Universal App
Here you will get some tips, how to create Universal Application.
Hope this will help you out.
REview this link may be helped you...
http://elusiveapps.com/blog/2011/10/converting-iphone-apps-to-univeral-ipad/
if the view controller can easily be used for both ....
MyViewController.h
MyViewController.m
MyViewController~ipad.xib
MyViewController~iphone.xib
Set the File's Owner in both the xib files to your UIViewController subclass and connect up the view + anything else you want connected.
When I init my view controller this is all that's required
MyViewController *viewController = [[MyViewController alloc] init];
// OR
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
The Apple docs for UIViewController state that if you provide a xib with the same name as the view controller it will be loaded. The ~ipad and ~iphone ensure that the correct xib is loaded.

how to change a ViewController to another that has no ".xib"?

I have a view controller that generates the ".xib" with Three20
I want a tableView Send me this ViewController has no ".xib" as you do?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.albumController) {
self.albumController = (AlThumbsViewController *)self; //AlThumbsViewController no have xib //Three20 is generated automatically with
}
[self.navigationController pushViewController:self.albumController animated:YES];
[self.albumController release];
self.albumController = nil;
}
Thanks for all.
MyViewController *view = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self.navigationController pushViewController:view animated:NO]; ---> you can use YES
or
[self.view addsubview:view.view];
and for opposite
[self.navigationController popViewControllerAnimated:NO];
[self.view romovefromsuperview];
If I understand you correctly, you want to transition from one view controller, to a second view controller which did not have a NIB/XIB file generated for it when you created the second view controller?
If so, you need to create a create a new XIB in xcode. Go to New File, User Interface, View. Then associate the new XIB file to the view controller by changing the Class Name in interface builder for your XIB to the same name as your view controller. Then set the view outlet in Interface Builder to the main view of the XIB. Then do initWithNibName on your view controller that does not have a XIB, and pass it the new XIB file you just created.
EDITED BASED ON COMMENT:
Step 1: Create a new file, User Interface, View. This will generate a stand alone XIB file.
Step 2: Go to your new XIB. Click File's Owner. Click Identity Inspector icon on right menu (the middle little icon). For "Class" change the name to the exact name of the view controller you want to link it to.
Step 3: Click file's owner. Click Connections Inspector icon on right menu (the right most little icon). Under Outlets, connect "view" to the main view of the XIB (just drag from the circle to the main window).
Step 4: Load your view controller like this:
MyViewController *view = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
As in the picture?
error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_ $ _AlThumbsViewController", Referenced from:
objc-class-ref in SocialesAlbumViewController.o
ld: symbol (s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use-v to see invocation)
see image here