When you create a new xcode project using a view based application template, here is the code in the app delegate for the viewController in the "application:didFinishLaunchingWithOptions"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
In Apple's Your First iOS Application Guide
which starts with the window based application template, we need to alloc and init our viewcontroller class and then point the app delegates viewcontroller ivar to that. Then release. See code below:
MyViewController *aViewController = [[MyViewController alloc]
initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
I can't find memory allocation or initialization anywhere in the view-based application template. What am I missing?
The initialization is made in the NIB (named in the plist), doing it again in code would be useless, while the second example doesn't create in NIB and, therefore, needs an initialization in code... or maybe I'm missing something.
Related
I'm working on an iOS 7.1 app on Xcode 5.1.1 (can't be upgraded currently), with ARC and without a StoryBoard, and when I call an empty method in the viewDidLoad method, the app crashes at the end of my custom method. Currently, I'm thinking that it's either my older version of Xcode, or the fact that I'm not using a StoryBoard, but I've simplified the code as much as possible and still cannot find the error. if someone could point out what I'm doing wrong, that would be great, thanks!
The crash just says Thread 1: breakpoint 1.1, crashing when [self.window makeKeyAndVisible] calls [viewController viewDidLoad].
ViewController.h
#interface XYZContactsTableViewController : UITableViewController
#end
ViewController.m:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self getAddressBook];
}
- (void)getAddressBook {
} // App crashes at line point exactly
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
XYZContactsTableViewController *viewController = [[XYZContactsTableViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Edits:
Also, calling pure C functions in the viewDidLoad method works, so the problem has something to do with the viewController object.
The crash just says Thread 1: breakpoint 1.1
Aha. You are not crashing at all. You are just pausing at a breakpoint. If you don't want to pause, or if breakpoints confuse you, take the breakpoint away or turn breakpoints off. Breakpoints are great, but you clearly don't understand them, so turn them off for now (but do learn to use them eventually, as they are extremely cool!).
why don't you use some already implemented component? :)
Check KBContactsSelection which allows you to search and select multiple contacts and is easily customizable using elegant Builder Pattern.
I'm trying to create a project in Objective-C language without storyboard with Xcode 6 Beta 5. I have tried and created a empty project but it didn't work as Xcode 5.
I have read this topic How to create project without story board in Xcode 6 - Swift but it didn't help me.
What do you mean it didn't work as Xcode 5? You can create empty project without storyboard and then add your own class with XIB like in Xcode 5:
File -> New -> File -> Cocoa Touch Class -> Set "Subclass of:" as (for example) UIViewController and check "Also create XIB file".
You first make a new "Single View" project. This starts out with a Storyboard, but as Xcode 6 has removed the option for creating an Empty project, we will just work with it from here.
You then create a new file for this project, go into the "User Interface" category and select "View". I would name it the same name as your original ViewController as it will replace the storyboard we are about to delete from the project.
Once the XIB is created, you will want to select it and set the "File's Owner" to point to the "ViewController" class that you want this XIB to link with. That is done by going into the Identity Inspector of the File's Owner of the Xib, and changing the default of NSObject to the class name of your view controller.
Once done with that, you want to go to the Connections Inspector to link the view of the File's Owner to the view of the XIB. Just click the little circle across from "view" and drag it over to your view to connect it. You should then have a connection between view and View.
Now the important parts. Go into your project Target, under the "General" tab. There is a subsection called "Deployment Info". In that subsection there is a field for "Main Interface". This field should be showing the name of the storyboard. You need to delete the value shown in this field, so that the Main Interface is left blank.
Then go into the App Delegate and set the root view controller of your window like you have been for previous versions of Xcode. Once that is done you should have a running app using your XIB, and you can delete your storyboard from the project without any adverse affects.
I don't know why people are down voting this as it is a legitimate question, so here is what you need to do:
Create an empty project, create a new view controller (File/New/File) - with XIB file if you need one, import the new view controller into your AppDelegate, and set this view controller as the root view controller.
AppDelegate.m:
#import "AppDelegate.h"
// import the view controller you want to be displayed first
#import "FirstViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// create an instance of the view controller you want to be displayed first
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
// set it as the root view controller of the application's window
[self.window setRootViewController:firstViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now, of course, if you wanted the create a tab bar or navigation controller, you would do this a bit differently, however this should be a good starting point for you.
AppDelegate.h
UINavigationController *nav;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *ll=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:ll];
[self.window setRootViewController:nav];
[nav setNavigationBarHidden:YES];
return YES;
}
It is very simple:
create empty application project
add to this project New File -> Objective-C class (with .xib file). My class is named "ViewController" :)
Now you must create UINavigationController in AppDelegate.h e.g.:
#property (strong, nonatomic) UINavigationController *navController;
than you must set your navcontroller in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
ViewController* homeViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
_navController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
self.navController.navigationBarHidden = YES;
self.window.rootViewController = self.navController;
return YES;
}
Thats all.
I've created a blank iPhone app project and would like to show a full-screen advertisement during app launch.
I tried to install the ad by following this guideline: https://github.com/mopub/mopub-ios-sdk/wiki/Interstitial-Integration-For-iOS
That's what I've done finally:
Actually all codes are just copied from the previous link.
However, an error shows when app runs:
Application windows are expected to have a root view controller at the end of application launch
I think this error may probably related to the loadView method, because if I remove the loadView method, the error disappeared.
In fact, this error seems common as it can be easily searched on the internet, but I don't know how loadView is related to it, and how can it be solved in my case.
Any solutions? Thanks a lot.
You probably need to do this:
Add
#import "ViewController.h"
to the top of AppDelegate.m
And in AppDelegate.m, your application:didFinishLaunchingWithOptions: method should have some code like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ... Other code
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
UIViewController *vc = [[UIViewController alloc] init];
[vc.view addSubview:self.tab_controller.view];
[self.window setRootViewController:vc];
OR
UIViewController *vc = [[UIViewController alloc] init];
[vc.view addSubview:yourClass.view];
[self.window setRootViewController:vc];
If you started with an empty template and added a storyboard, you need to do a couple of things:
You need to delete all the lines (except return statement) inside didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
In project settings ->General, select your storyboard as the main interface
Attached snapshot to help you
At the right side check there is one option under attribute inspector which asks to set as "is rootView controller"
I want to use storyboard in my old project (which was implemented using xibs) for adding a new features.
I have created storyboard file and added a view controller to it and in Identity inspector I have specified the class name for the view controller.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES
}
#end
In the firstviewcontroller when i click on a button it will call the gotoSomeviewController method where it pushes to the eventslistViewController loading from EventsScreen storyboard
#implementation FirstViewController
-(void)gotoSomeviewController
{
EventsListViewController *vc = [[UIStoryboard storyboardWithName:#"EventsScreen" bundle:nil] instantiateInitialViewController];
[controller pushViewController:vc animated:YES];
}
#end
when I'm running application the EventsListViewController is showing a empty screen without the views I have added in storyboard.
Based on the information supplied there should be no reason why you mix NIBs and Storyboard.
I would setup just a storyboard. Remove the app Delegate code and setup the project to load the storyboard for the specific device.
Setup your storyboard to have the initial view controller as a UINavigationController that has the FirstViewController as the root of that.
Then you can simply drag to the SecondViewController from the button and select push as the move option. Then remove your method for gotoviewcontroller. There is no reason why you would have this setup like this...
I wanted to get rid of the storyboard and make everything in nib files. So I removed the main story board and coded the launch routine in the app delegate. I also deleted the storyboard name in the summary section of the app target so the complier won't complain. However, now nothing is shown when the app is launch, only a black blank screen. Is there away to fix this without creating a new non-storyboard project?
Here is the code in my app delegate..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MasterViewController *mainController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:mainController animated:YES];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Edit your Info.plist to indicate the main nib file and not a storyboard file. You'll need to change both the key (UIMainStoryboardFile to NSMainNibFile) and the associated value.
When you deleted the main story board you also deleted the StoryBoard Entry Point. To fix this, replace the StoryBoard Entry Point and you should get the desired display.
Thanks,
Rajat
StoryBoard Entry Point