OS X how to designate startup viewcontroller - objective-c

I am developing a simple browser project on mac, i didn't use the default viewcontroller.
Instead, I write a viewcontroller class BrowserViewController.
and write follow in appdelegate
#interface AppDelegate()
#property (nonatomic, strong) BrowserViewController *browserController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
self.browserController = [[BrowserViewController alloc] init];
[self.window makeKeyWindow];
[self.window setContentView:self.browserController.view];
}
#end
But when the app start up it lead to the default viewcontroller not the BrowserViewController. I really don't know the reason.
Thanks for the help, i have solve this problem. My solution is like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//set the frame of the window fit to the device's frame
//
self.window = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO ];
//
//
self.browserController = [[BrowserViewController alloc] init];
//set contentView
//
self.window.contentViewController = self.browserController;
//this is setting global backgroundColor of the window
//
self.window.backgroundColor = [NSColor whiteColor];
//this means the window is the window that will receive user interaction.
//
[self.window makeKeyAndOrderFront:self];
//[self.window makeKeyWindow];//NOTE: This is not working.
}

You can select Is Initial Controller in the storyboard.

Sounds like you're looking for (inside your didFinishLaunchingWithOptions method):
self.browserController = [[BrowserViewController alloc] init];
[self.window setRootViewController: self.browserController];
[self.window makeKeyAndVisible];

If you want it programmatically:
Note
This is for iOS, but the logic are identical. Hope this will be useful for some reason.. :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//set the frame of the window fit to the device's frame
//
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//what you have above is correct
//
self.browserController = [[BrowserViewController alloc] init];
//you can also directly set it like
//
BrowserViewController *mainView = [[BrowserViewController alloc] init];
//this is the most important, you need to set the window's rootViewController
//
self.window.rootViewController = mainView;
//this is setting global backgroundColor of the window
//
self.window.backgroundColor = [UIColor whiteColor];
//this means the window is the window that will receive user interaction.
//
[self.window makeKeyAndVisible];
return YES;
}
Hope this is helpful and informative.. Cheers! :)

Related

how I use MFSideMenu?

I'm trying to create a MFSideMenu however I never used it and do not know what it missing. Can anyone write a short tutorial on how to finalize it?
-(void)viewDidLoad{
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
}
In AppDelegate.h
#import "MFSideMenuContainerViewController.h"
In AppDelegate.m
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"navigationController"]; //I have instantiated using storyboard id.
UIViewController *leftSideMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"sideMenuViewController"]; //I have instantiated using storyboard id.
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
In Your controller.m add:
- (IBAction)menuPressed:(id)sender
{
[self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}
- (MFSideMenuContainerViewController *)menuContainerViewController
{
return (MFSideMenuContainerViewController*)self.navigationController.parentViewController;
}
Put this code into your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// init center, left, and right view controllers
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController]
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}

Managing navigation controllers / view controllers

Not sure how to title this question, but i'v got such a problem: Up until now i my app runs mainly in one navigationcontroller with table views. But now i'm trying to integrate dropdown settings menu, and can't get it properly done.
The way i'v done now and it works
The changeController is called from one button. ChangeController is in appdelegate.
- (void) ChangeController
{
self.window.backgroundColor = [UIColor blackColor];
DropDownExample *e = [[DropDownExample alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:e];
[e release];
[self.window addSubview:self.navigationController.view];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
}
But this approach has consequances - there is no transition if button is pressed, the settings menu appears instantly, you cannot go back via navigation bar above (nothing there).
So how to do this properly?? I'm new to ios, so just tell me the whole idea how to do this.
Didfinishlaunchingwithoptions method from appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease
];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
TableViewController *tableVC = [[TableViewController alloc] initWithNibName:#"TableView" bundle:nil andType:CONTROLLER_TYPE_FIRST];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:tableVC];
self.navigationController = navC;
[tableVC release];
[navC release];
self.window.rootViewController = _navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Ok, here's the answer. Write the method changeController in the same class where the Button exists which calls changeController
In the method, write this.
- (void) ChangeController
{
DropDownExample *e = [[DropDownExample alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:e animated:YES];
[e release];
}
What you want is to insert new UIViewController on the top of the present Stack. If you would be having a navigation Bar at the top by default then there would be a back Btn by default, which would pop up that controller.

No visible interface for #LWWFirstController declares the selector initWithStyle

I am getting the error "No visible interface for #LWWFirstController declares the selector initWithStyle". I have written similar code for setting up my Navigation Controller and it worked fine, but I can't seem to find what is causing this error. I have looked on StackOverFlow for similar issues but none fit, I'm using the correct method for it. Does anyone have in clue? Here is my code below.
#import "LWWAppDelegate.h"
#import "LWWFirstLevelController.h"
#implementation LWWAppDelegate
#synthesize window = _window;
#synthesize navController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
LWWFirstLevelController *first = [[LWWFirstLevelController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc]
initWithRootViewController:first];
[self.window addSubview:navController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
It basically says you didn't implement initWithStyle method in your LWWFirstLevelController.
Are you sure it is a subclass of UITableViewController?

How can I change navigationBar Background

I try change to background of navigationBar.
Search.h:
#import <UIKit/UIKit.h>
#interface Search : UIViewController
#end
Search.m:
#import "Search.h"
#implementation Search
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image = [UIImage imageNamed:#"navigation_header.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
I can't change navigation background.
Thanks.
This is actually simpler to do then you think. In your app delegate...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
return YES;
}
This will universally change your navigation bar background image.
Note: I believe UINavigationBar's "appearance" property is available in iOS 5 and up.
viewController.h
IBOutlet UINavigationBar *navBar;
viewController.m
//for color
- (void)viewDidLoad{
[super viewDidLoad];
[navBar setTintColor:[UIColor colorWithRed:0.0/255.0 green:100.0/255.0 blue:20.0/255.0 alpha:1.0]];
}
//for image
UIImage *image = [UIImage imageNamed:#"navigation_header.png"];
[self.navigationController.navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[Search alloc] initWithNibName:#"Search" bundle:nil] autorelease];
navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
}
Write this method in AppDelegate.m. I think it will be helpful to you.

BookController in iOS 5 Developer's Cookbook (Erica Sadun) does not start correctly in landscape mode

The problem I'm trying to solve:
I used chapter 05 example 06 BookController in chapter 5 (C05/06-Scrubbing Pages in https://github.com/erica/iOS-5-Cookbook.git ) in a project that have a UInavigatioController as rootViewController, but it does not start correctly in landscape mode. If you rotate the device portrait and return to landscape it begins to work.
To show the bug modify the example test bed delegate in main.c with the following:
#pragma mark Application Setup
#interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
#end
#implementation TestBedAppDelegate
TestBedViewController *tbvc;
UINavigationController *navigationController;
- (void) pushBookController: (UIGestureRecognizer *) recognizer
{
[navigationController pushViewController:tbvc animated:YES];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tbvc = [[TestBedViewController alloc] init];
UIViewController *start = [tbvc controllerWithColor:[UIColor whiteColor] withName:#"white"];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pushBookController:)];
[start.view addGestureRecognizer:tap];
navigationController = [[UINavigationController alloc] initWithRootViewController:start];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
return YES;
}
#end
My application starts landscape, so the problem is that the book controller does not recognize landscape mode at start (if you rotate after start it will work). Try it.
To recognize landscape at start, substitute in BookController the following method
// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
//[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self fetchControllersForPage:requestedPage orientation:self.interfaceOrientation];
}
and add the following:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {return YES;}
Now the problem is that BookController does not appear at start, but again if you rotate it begins to work.
probably you solved this already, but try changing the Book's instantiation like this:
+ (id) bookWithDelegate: (id) theDelegate
{
NSDictionary *options = nil;
//Check the orientation
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Setting Spine Location to middle for Landscape orientation
options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];
}
BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
bc.dataSource = bc;
bc.delegate = bc;
bc.bookDelegate = theDelegate;
return bc;
}
Note that I'm adding a statusBarOrientation check to set the spine.
Basically what you need is to check the orientation when you're creating the UIPageViewController, and I used the statusBarOrientation.
Oh, and don't forget to ensure you're getting the right orientation
- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
// in case the UIInterfaceOrientation is 0.
if (!orientation) {
isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
return isLandscape;
}
ps: The bookWithDelegate method corresponds to the BookController class within the example mentioned in the question.