Where to declare ViewControllers for a custom tab view? - objective-c

I'm trying to figure out how to use a custom tab view I found called JMTabView by Jason Morrissey on GitHub. (I tried asking him directly but got no response.)
I know how to programmatically create a UITabBarController and assign view controllers. What I can't figure out is where to declare my four UITableViewControllers and other VC's for each of the four tabs in this example. The code:
// TabDemoAppDelegate.m -> do I declare them here?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TabDemoViewController * demoViewController = [[[TabDemoViewController alloc] initWithNibName:nil bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:demoViewController] autorelease];
//[self.navigationController setNavigationBarHidden:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
// TabDemoViewController.m -> or somewhere in here?
-(void)addCustomTabView; { // this is a private method
JMTabView * tabView = [[[JMTabView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 60., self.view.bounds.size.width, 60.)] autorelease];
tabView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[tabView setDelegate:self];
UIImage * standardIcon = [UIImage imageNamed:#"icon3.png"];
UIImage * highlightedIcon = [UIImage imageNamed:#"icon2.png"];
CustomTabItem * tabItem1 = [CustomTabItem tabItemWithTitle:#"One" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem2 = [CustomTabItem tabItemWithTitle:#"Two" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem3 = [CustomTabItem tabItemWithTitle:#"Three" icon:standardIcon alternateIcon:highlightedIcon];
CustomTabItem * tabItem4 = [CustomTabItem tabItemWithTitle:#"Four" icon:standardIcon alternateIcon:highlightedIcon];
[tabView addTabItem:tabItem1];
[tabView addTabItem:tabItem2];
[tabView addTabItem:tabItem3];
[tabView addTabItem:tabItem4];
[tabView setSelectionView:[CustomSelectionView createSelectionView]];
[tabView setItemSpacing:1.];
[tabView setBackgroundLayer:[[[CustomBackgroundLayer alloc] init] autorelease]];
[tabView setSelectedIndex:0];
[self.view addSubview:tabView];
}
He mentions blocks... if they're relevant, what are they and is this where I would declare the VC's? If so, how?
// You can run blocks by specifiying an executeBlock: paremeter
// #if NS_BLOCKS_AVAILABLE
// [tabView addTabItemWithTitle:#"One" icon:nil executeBlock:^{NSLog(#"abc");}];
// #endif

I'm not familiar with that particular library, but if it follows a similar pattern to UITabViewController I would create properties for your tab (if you need to reference them later) in your App Delegate's .h file and then create your instances in the .m. If you are just placing the tabs and don't need to reference them directly anymore you should be able to define them in the .m (probably in applicationDidFinishLaunching) and then assign them as tabs and let the tab controller take it from there.

Related

JASidePanel : Need to maintain permanently a menu to the left

I'm studying the JASidePanel stackController.
But I would like to know if there is a possibility to maintain permanently a menu to the left of the screen independently of the JASidePanel.
In order to do that, I have tried to insert the JASidePanelController as a subview of an other UIViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// My part ---------------------
UIViewController *rootViewController = [ [ UIViewController alloc ] init ];
UIView *rootView = [ [ UIView alloc] initWithFrame:CGRectZero ];
rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth + UIViewAutoresizingFlexibleHeight;
[rootView setBackgroundColor:[UIColor yellowColor]];
rootViewController.view = rootView;
// ---------------------
JASidePanelController *sidePanelController =[[JASidePanelController alloc] init];
sidePanelController.leftFixedWidth = 260;
self.viewController = sidePanelController;
self.viewController.shouldDelegateAutorotateToVisiblePanel = NO;
self.viewController.leftPanel = [[JALeftViewController alloc] init];
self.viewController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[JACenterViewController alloc] init]];
self.viewController.rightPanel = [[JARightViewController alloc] init];
self.viewController.view.frame = CGRectMake(61, 0, self.viewController.view.frame.size.width, self.viewController.view.frame.size.height );
[ rootView addSubview:self.viewController.view ];
// My part ---------------------
self.window.rootViewController = rootViewController;
// ---------------------
[self.window makeKeyAndVisible];
return YES;
}
It works but I am not fully satisfied.
Indeed, when I swipes my finger to the left, in order to show the right panel, the center panel don't move correctly to the left and covers my personal menu (in yellow).
As a result, the right panel is not visible totally and my menu (in yellow) is not visible.
Please, have a look to my illustrations to understand what I want to do.
Is anyone have a idea of how to do that?
Many thanks per advance!

Navigation controller and no storyboards

I am writing an app in Objective C. For this project I cannot use ARC or storyboards. It should have 6 views. Firstly, the navigation controller shall keep table view (it will contain some data passed in from an array). In the toolbar on the top once the item has been pressed the app should move to second view.
I know that I need to code that in my delegate file, but I a not quite sure what needs to be included. Those were the requirements.
In fact when I run my app it does not show that navigation controller which got table view as my entry view. In the settings I cannot find a checkbox in order to select it and make it an entry view.
Any suggestions?
Best regards
Best way to use UITabBarController
Firsts Create all object of UIViewController and UINavigationController in AppDelegate.h file and use following method of AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];
self.viewCon=[[ViewController alloc] init];
self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor=[UIColor blackColor];
self.viewCon.title=#"First View";
self.fView=[[FirstViewController alloc] init];
self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
self.FnavCon.navigationBar.tintColor=[UIColor blackColor];
self.fView.title=#"Secound View";
self.sView=[[SecoundViewController alloc] init];
self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
self.sView.title=#"Third View";
.
.
// create UIViewController and UINavigationController As you need
.
.
.
UIImage *img1=[UIImage imageNamed:#"Australia.gif"];
self.tbItem1=[[UITabBarItem alloc] initWithTitle:#"First Page" image:img1 tag:1];
self.viewCon.tabBarItem=self.tbItem1;
UIImage *img2=[UIImage imageNamed:#"Cameroon.gif"];
self.tbItem2=[[UITabBarItem alloc] initWithTitle:#"Secound Page" image:img2 tag:2];
self.fView.tabBarItem=self.tbItem2;
UIImage *img3=[UIImage imageNamed:#"Canada.png"];
self.tbItem3=[[UITabBarItem alloc] initWithTitle:#"Third Page" image:img3 tag:3];
self.sView.tabBarItem=self.tbItem3;
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
[viewArr addObject:self.FnavCon];
[viewArr addObject:self.SnavCon];
self.tbCon=[[UITabBarController alloc] init];
self.tbCon.viewControllers=viewArr;
[self.window addSubview:tbCon.view];
[self.window makeKeyAndVisible];
return YES;
}

Thread 1: EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0) error

I have an iPad app I am making, but it crashes on startup even though there are no errors or warnings, the output doesn't output anything besides "(lldb)", and it marks a pointer.
This is the image of the post build crash.
And here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* UIImage *myImage = [UIImage imageNamed:#"de_dust2.png"];
myImageView = [[UIImageView alloc] initWithImage:myImage];
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 0.75;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
// [myScrollView addSubview:myImageView];*/
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.viewController setRed:1];
[self.viewController setGreen:0];
[self.viewController setBlue:0];
[self.viewController setAlpha:1];
[self.viewController checkRotation];
return YES;
}
I now also noticed that the error was given at the [self.window makeKeyAndVisible]; line.
Your code looks ok, the crash could be caused by some exception thrown parsing the XIB file of the window ...
Take a look at the Window in Interface Builder and try to temporary remove all reference to any IBOutlet present in the View Controller ...

How to present a (no nib) ViewController

I've created a ViewController (without nib files), so how do I load this viewController in the app delegtate? my current code is:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
And how can I use the presentModalViewController method to show new view controllers of the same "CustomViewController" class?
THANKS for the help!
NOTE: By the way, I do see that this code does call my initialization method, and my ViewDidLoad method in my customViewController is being called however the screen is still black....
It looks like your properties are not set up correctly.
Check this line:
viewController = [[MyCustomViewController alloc] init...]; // I use a custom init method
self.window.rootViewController = self.viewController;
check that self.viewController is okay, and there's nothing stopping you from just doing:
self.window.rootViewController = viewController;
Whoops, I was just missing this code in my viewDidLoad Method:
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.backgroundColor = [UIColor whiteColor];
So everything was working, but the screen by default is black and without a frame.
I had to do just that in one of the apps I'm working on. Try typecasting your view controller
CDViewController *viewController = (CDViewController*)self.window.rootViewController;
viewController.managedObjectContext = self.managedObjectContext;
viewController.managedObjectModel = self.managedObjectModel;
// Override point for customization after application launch.
return YES;

Creating a common UIToolbar helper file and methods for Objective C

I have build a view with a UIToolbar which is working great.
This toolbar will be appearing right across the app, and right now I am copy/pasting the code into lots of different files.
I do not want to repeat myself, and am looking to create a helper file that will include the toolbar setup and the methods linked to the toolbar in every file I need.
I've tried putting the following code into a .h .m file and inheriting from UIView, but there is a problem because there is a reference to self.navigiationItem
Is there a way that I can create a common Objective C file that will have all the code and methods I want to use?
Thanks.
- (void)viewDidLoad
// ...
// appears in viewDidLoad
// ---- TOOLBAR -----------//
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100.0, 44.01f)];
//[toolbar setBackgroundColor:[UIColor blackColor]];
//[toolbar setTintColor:[UIColor redColor]];
//[toolbar.layer setBorderColor:[[UIColor redColor] CGColor]];
// Bar buttons
UIBarButtonItem *barReloadBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(btnReload:)];
[barReloadBtn setStyle:UIBarButtonItemStyleBordered];
// Profile bar button
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"111-user" ofType:#"png"]];
UIBarButtonItem *barProfileBtn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:#selector(btnProfile:)];
// Button array
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:barProfileBtn];
[buttons addObject:barReloadBtn];
[toolbar setItems:buttons];
// Set nav items
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
// memory cleanup
[image release];
[buttons release];
[barReloadBtn release];
[barProfileBtn release];
[toolbar release];
// ---- /TOOLBAR -----------//
}
#pragma mark - IBActions
-(IBAction) btnProfile:(id)sender
{
UserProfileVC *userProfileVC = [[UserProfileVC alloc] initWithNibName:#"UserProfileVC" bundle:[NSBundle mainBundle]];
UINavigationController *tmpNavCon = [[UINavigationController alloc] initWithRootViewController:userProfileVC];
[self.navigationController presentModalViewController:tmpNavCon animated:YES];
[tmpNavCon release];
[userProfileVC release];
}
-(IBAction) btnReload:(id)sender
{
NSLog(#"Not done yet");
}
navigationItem is a property of UIViewController, not UIView. If you've got common functionality like this, I would inherit from UIViewController, add your custom logic to viewDidLoad (or wherever is appropriate) and then inherit your view controllers from that class. Just make sure you call [super viewDidLoad] from your subclasses' implementations of viewDidLoad.