File's owner belongs to the wrong class - iphone

My iPhone app had the standard ViewController.h, ViewController.m and ViewController.xib to support the 3.5" retina display. I made a new ViewController_5.h, ViewController_5.m, ViewController_5.xib to support the 4" display on the iPhone 5. In theory, these should be totally independent of each other. But now, IBActions from the ViewController_5.h show up in ViewController_5.xib, and when I connect them I expect the action to carry out whatever is in ViewController_5.m. But it actually does the code in ViewController.m. What's going on here?

from the code you posted it looks like you just need to supply the right view controller class (ViewController_5) in the else part of your if statement, like this
... else { self.viewController = [[ViewController_5 alloc] initWithNibName:#"ViewController_5" bundle:nil]; }

Related

How do I make my app universal? (3.5", 4", and iPad)

I'm brand new in Xcode and have been able to scoot by with some fairly simple apps thanks to my previous programming experience, the Storyboard in Xcode, and most of all THIS WEBSITE.
One thing I haven't been able to figure out is how to make my app universal? The way I have my app set up is that it looks a lot like the iPhone home screen with pages and app icons. However, when I hit the little "Change to iPhone 5" button, 1) It only changes my FirstViewController and 2)My icons are all out of alignment.
Do I make another storyboard (If so, how?)? Do I make another view controller for each screen resolution? For either of those questions do I program a test to see which device I'm using and for it to choose the correct storyboard or ViewController? My app is currently set to universal, but I still haven't even been able to find the iPad resolution option for view controllers and stuff.
Please be as simplistic as you can. I have only been doing this for an extremely short time. Thanks for all your help both here and around this site!
So In order to get the iPad version working, follow these steps:
First go to your target settings, scroll down to "Deployment Info", and change it from iPhone to Universal
Next you will need to create a new Storyboard file. On the bar at the top go File>New>File and this thing should pop up:
After you have selected iOS>User Interface>Storyboard as shown above, click next and it will ask iPhone or iPad. After selecting iPad, the new file will be ready to map your interface for iPad. However, you need to make sure the app uses it. So go back to your target settings and scroll back down to "Deployment Info" and switch to iPad:
Set the "Main Interface" option to the name of your new storyboard file.
As for preparing for iPhone 5, that part can get a bit annoying. I find that the additional space is too small for any major additions but too large to be ignored easily. How you deal with it will vary greatly depending on what you app does. From what you described, with app icons like the iPhone screen, you will probably want to have an additional row of icons, just like the iOS. To do this, you can either manage it programatically or you may want to have seperate storyboards for iPhone 4 vs iPhone 5. If you want the separate storyboards, this question will have your solution.
To initialize storyboard:
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
UIViewController *initialViewController;
if (iOSDeviceScreenSize.height == 480)
{
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:#"iPhone4" bundle:nil];
// Instantiate the initial view controller object from the storyboard
initialViewController = [iPhone35Storyboard instantiateInitialViewController];
}
if (iOSDeviceScreenSize.height == 568)
{
// iPhone 5 and iPod Touch 5th generation: 4 inch screen
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:#"iPhone5" bundle:nil];
initialViewController = [iPhone4Storyboard instantiateInitialViewController];
}
self.window.rootViewController = initialViewController;
[self.window makeKeyAndVisible];
return YES;

Root View Controller Error

I'm working along with the book Beginning iPhone 3 Development, and am running into some issues on the "Pickers" app. I'm using Xcode 4.2, and I set it up exactly like they have it set up in their source code. All the code is the same, and all the outlet connections are the same. But, when I run my version, it launches a black screen, and the debugger says "Applications are expected to have a root view controller at the end of application launch."
There version in the source code runs just fine, and mine looks identical to it, but for some reason mine just won't run. I've Googled this issue and people have a bunch of workarounds, but I feel like there is something really simple in IB that I'm not seeing.
Any help would be great, thanks.
The iPhone 3 book is probably having you add the view of your view controller as a subview of the window, correct? Well, since iOS 4, UIWindow now has a rootViewController property and setting this property to your initial view controller is now the preferred way to get your first view controller on screen.
Basically replace something like this in -application:didFinishLaunchingWithOptions: in your application delegate...
[self.window addSubview:viewController.view];
with this...
self.window.rootViewController = viewController;
Quite a bit has changed since iPhone OS 3; beware as you proceed through the book.
Do you have the RootViewControllers XIB-file? And is it connected to the RootViewController Class (in designer)

xCode - Changing views between 2 existing xib-files

I'm very new to xCode and objective-C so I wanted to make a simple textRPG.
I'm currently making a character creation process consisting of 4 xib-files. The only way I got switching views to work was to look at the utility-template. Problem is, now I have the first screen being the delegate for the second screen, being the delegate for the third screen etc. So by the end of the character creation process I can't dismiss the views because that just "steps back" through the views.
When I've searched around for a solution I've found a addSubview-method but it seems like that makes a new view, like, empty to arrange programmatically.
All I need is a simple way to switch from one loaded xib to another xib. Have I misunderstood addSubview or do I need something completely different?
(If it helps: I've worked with VB for several years, in case you notice that I missed some kind of concept concerning views and such)
Thanks in advance! :)
Use this code. It is really simple and works well.
View *view = [[View alloc] initWithNibName:#"xibNameGoesHere" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];
This will switch to another xib file and the two views won't own one another. I am using it in my own game right now.
#Joakim Ok, this is the way I do it. I have a class called RootViewController : UIViewContoller, whose view I add directly to the window. Then I make a subclass of UIView i.e. MyView. All of my views then subclass MyView. I also make an enum for all my views. In RootViewController you should have a switchView:(int)view method that looks something like this:
-(void) switchView:(myView) view
{
[_currentView removeFromSuperview];
switch(view)
{
case titleView:
_currentView = [[TitleView alloc] initWithRoot:self];
break;
case homeView:
_currentView = [[HomeView alloc] initWithRoot:self];
break;
default: break;
}
[self.view addSubview:_currentView];
[_currentView release];
}
in #interface RootViewContoller define MyView *_currentView;
TitleView and HomeView both subclass MyView and have a common method -(id)initWithRoot:(RootViewController*) rvc.
To switch between views use [_rvc switchView:homeView];
Hope this helps :)
It is called UINavigationController. Idea is
1) You push corresponding 'next' controller into navigation controller each time user submits current screen. Bonus - you'll get 'back' button for free on each step of character creation.
2) After character is created you pop all character creation controllers from stack.
Please read View Controller Programming Guide for iOS before trying to 'switch views' and such. You'll save tons of time and nerves.
another idea is not to use interface builder at all. i have been working with iphone apps for two years now and found that interface builder really prolongs the time to actually make something. make your own root controller and think about the logic you need to navigate through the views.

Switch Views with button in iPhone App

Firstly, I am an iPhone programming noob (I know that's not very helpful). But I do speak english :).
I am making an app that is basically a soundboard, that is you press a button and it plays a sound effect.
One thing I wanted to add was a view which came up when a sound was playing, that said something like "A sound is currently being played, please tap anywhere to stop". The way I did this was take a screenshot of the normal screen, put a transparent black square over it in Microsoft Word (I know, great graphics program, right?) and use that. It looks fine.
I was wondering how to make it so that when you press a button to play the sound, not only does it play the sound, but it also switches views to the second view described above. Then, if the person touched the screen in the second view, it would switch back to the first view, and stop playing (I assume the best way to do this would be to have some kind of 'stop playing" method in the "viewDidLoad" section of the first view).
I would very much appreciate it if someone could tell me how to make an action which is called when a button is pushed, that switches views. I don't need any fancy navigation bar staying up or whatever, I just need it to switch views.
Also, my application is a view based application, and I currently have the following files:
Classes:
(appname)appdelegate (.h & .m),
(appname)viewcontroller (.h & .m), (my main window)
playingController (.h & .m), (my second window)
Resources:
(appname)viewcontroller.xib (main window IB file)
playing.nib (second window IB file)
mainwindow.nib (no Idea what this does, don't really care).
I think that these will work for the app, but I'm probably totally wrong. Please tell me if I need additional files, or if I need to rename these files.
Thanks a lot,
Luke
Try placing an invisible button over the entire view to get back to the first view
to get to any screen use something like this:
SecondScreen *second = [[SecondScreen alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
Steps:
1 Go to interface builder (double click .xib file)
2 Drag a UIButton on screen
3 Go to code
4 Go to .h file and write
-(IBAction) switchViews:(id)sender;
5 GO to .m file and type in
-(IBAction) switchViews:(id)sender
{
SecondScreen *second = [[SecondScreen alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
6 In interface builder, click the button, Hold CONTROL and Drag all the way (a line shows) to the FileOwner.
7 Choose switchViews method from list
gl
You need to create a new UIView, controlled by a UIViewController and presented with presentModalViewController
You need a UIButton, which can be set up either programatically or within IB that will call presentModalViewController
Once your new view is visible, you can set it up however you wish in terms of your 'Stop Playing' action. To remove it, dismissModalViewController is what you're looking for.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/
http://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/
http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/

Iphone changing to another view [BEGINNER]

Hi I have a small doubt, I have 3 Nib Files:
ConfigureContacts.xib
CallContactsViewController.xib
MainWindow.xib
When Application starts I do:
[window addSubview:callContactsViewController.view];
[window makeKeyAndVisible];
So that the CallContactsViewController.xib is loaded.
Inside CallContactsViewController.xib there is a button, that when presses jumps to:
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self.view addSubview:configureContacts.view];
}
The idea is that when the button is pressed it goes to the "next window" which is the other .xib file. Is this the correct way of changing through xibs ? Am I wasting memory here ?
Thanks in advance
is ConfigureContacts a ViewController subclass? (It looks like it is)
if so you would actually do something like this
-(IBAction)configureContacts:(id)sender
{
configureContacts = [[ConfigureContacts alloc] initWithNibName:#"ConfigureContacts" bundle:nil];
[self pushviewController:configureContacts animated:YES];
}
My Guess this is what you want to do.
[self.view addSubview:configureContacts.view] should work as well however it will change only part of the view and keep you on the same 'Window' so to speak.
pushViewController is a method from UINavigationController, which is used for managing hierarchical viewControllers. It sounds like that might be what you're trying to do here, in which case adding a UINavigationController to your code might be the way to go.
Alternatively, you could implement a third UIViewController that owns both CallContrats and ConfigureContacts and is responsible for switching between them.
Check out the sample code from Beginning iPhone Development 3. Specifically, check out the programs "06 View Switcher" and "09 Nav". I think they'll have the code that you're looking for.