changing default view with master/detail application - objective-c

can you help me with this one? I'm creating an app and I'm using a master/detail application, then I added a new class called MenuViewController and I want it to be the initial view to load when my app starts. I was able to do it but my MasterViewController does not work any more, everytime i click an item in the table view it's suppose to show another view called DetailViewController but ever since I changed the default view it doesn't show the DetailViewController anymore
Here's a piece of code i've done
in the appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
MenuViewController *menuView = [[[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil]autorelease];
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = menuView;
[self.window makeKeyAndVisible];
return YES;
}
and in my MenuViewController.m
// Created by Jan Arman Capistrano on 2/6/13.
// Copyright (c) 2013 Jan Arman Capistrano. All rights reserved.
//
#import "MenuViewController.h"
#import "MasterViewController.h"
#interface MenuViewController ()
#end
#implementation MenuViewController
-(IBAction)nextView:(id)sender
{
MasterViewController *masterViewc = [[MasterViewController alloc]initWithNibName:nil bundle:nil];
[self presentViewController:masterViewc animated:YES completion:nil];
}

It seems your MasterViewController is instantiated from a XIB. If so, check that the UITableView has its delegate and datasource bindings (propably the MasterViewController itself)
have you tried to set a breakPoint in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, to see if it's actually called ?

Related

Impossible to presentViewController

I've created a new application view based.
Here some the main code:
// AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
MIAPreferences *preferences;
}
#property (strong, nonatomic) UINavigationController *navigationController;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) MIAPreferences *preferences;
// AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize navigationController;
#synthesize preferences;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
// Override point for customization after application launch.
UIViewController *rootController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
navigationController.navigationBar.hidden = YES;
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
// HomeViewController.m
-(IBAction)openLogin
{
LoginViewController *view = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
// 1
[self.navigationController presentViewController:view animated:YES completion:nil];
// 2
[self.navigationController pushViewController:view animated:YES];
// 3
[self presentViewController:view animated:YES completion:nil];
}
All 3 options returns a EXC_BAD_ACCESS (code=2, address=....)
Can you please help me understand how to solve this?
UPDATE
The problem was due to a UIButton apparence set up during AddDelegate loading....but the first button was in the UIView I was going to load... that's why it crashed -.-"
It could either mean: (1) The pointer used to point to memory that was ok, but its chunk was deallocated or (2) The pointer is corrupt. You can try to use zombie mode, if you aren't already, to get more information. In XCode press the Command, option, and I keys and a screen should pop up. Selected the Run option in the left hand side, then Diagnostics, and under memory management enable zombie objects should be checked.
Check the value of view after initWithNibName:. init'ing with a nil nib name looks wrong to me, and you may be trying to push a nil view controller onto your navigation stack.

Add a UITabBarController to a UIViewController (Xcode 4.6)

I am studying Objective-C and I started to create an App while I am learning it.
I have created a "Single View Application" with two "View Controllers" called "ViewController" and "secondViewController", after that I thought to add an "UITabBarController", so following a tutorial I used the following code in the AppDelegate.m
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Artists" image:[UIImage imageNamed:#"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];
UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Music" image:[UIImage imageNamed:#"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Here is the ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
If I run the application, the tab bar is working but I have a blank screen and not the ViewController I have created.
Just because I am a noob, could you please tell me how to link or show the view controllers respectively in each tab?
Please be so kind to say everything also the obvious things.
Thank you in advance
Since you are beginner I would highly suggest you to use storyboards, that will help to manage the interface much more easier because you will not have to code almost anything.
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
I am just guessing but your problem might be that you directly init your view controller , you should call initWithNibName: method in your didFinishLaunchingWithOptions. The initWithNibName: is something that you call when you create a controller instance from a nib file.
So basically initWithNibName: is called when the NIB is loaded and instantiated.
I dont know your ViewContoller class names but you should change your view controllers init methods
in your app delegate.m
#import "ViewController.h"
#import "secondViewController.h"
UIViewController *viewController1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController
UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController
and please use Upper Case Chars and detailed names for class name secondViewController is not good programming practice
The tabbar controller shows the views, you created in your code. That's the expected behaviour, since you are creating completely new views.
If you created "ViewController" and "secondViewController" in Interface Builder, then you'll have to load the nibs - instead of creating new views - and tell the tabbar controller to use these.
Basically you'll have to change your lines:
UIViewController *viewController1 = [[UIViewController alloc] init];
to something like
UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:#"view1" bundle:nil];
Please see initWithNibName:bundle: for the details.

How to put a tab bar only in certain place in the app?

I want to put a tab bar for one place in my app. From there i can load some other view controllers, but that's it. I don't want it for my whole application. All examples I read through are making the tab bar controller as the rootviewcontroller of the window.
So how do i do that?
Thank you!
his may help you, Consider your view controller is HomeView where from you are going to push a tab bar controller, The HomeView is loaded in view at below:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HomeView *homeview = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:homeview];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
#end
then the .h, .m and .xib files of HomeView are going to be as follows:
In HomeView.h
#import <UIKit/UIKit.h>
#interface HomeView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and the HomeView.m is:
#implementation HomeView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)loadTabBar:(id)sender
{
[self.navigationController pushViewController:tabBarController animated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
and the .xib file must be as follows:
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController. Moreover that the HomeView must be inside a UINavigationController.
If not clarified with this answer, I'll update with detailed explanation.
The above is a way of loading tab bar controller using XIB method.
You can do this by coding like below by changing something in IBAction(button action) , In HomeView.m file:
#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation HomeView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
You have to make tabBar in app delegate and then add when ever you want using this
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
//[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}

Unable to display tableView in iOS app

I am fairly new to iOS and am attempting to display a tableView controller from another tableView controller in a drill down routine for an iPad app. However, the new tableView will not display. I can follow the program logic through the following routine in debug mode but after this logic, the same view remains on the screen. I set breakpoints in the new tableview program to be displayed and they are never reached. I have included the HEDView.h in the application file for this program and have no clue why the new view is not displayed. Any help is or suggestions for more info is appreciated.
Here is the routine to call the tableView: HEDView will not display.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
[tableView deselectRowAtIndexPath:indexPath animated:NO];
HEDView *detailViewController = [[HEDView alloc] initWithNibName:#"HEDView" bundle:nil];
// Pass the selected object to the new view controller.
detailViewController.title = #"HEDView";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
If your requirement is to navigate to other viewcontroller when cell in the row is selected, then I think your navigationcontroller is not properly allocated.While debugging check whether self.navigationController is returning proper address.If not then you have to first properly allocate it.
and one more thing, HEDView is UIViewController so you should follow proper naming convention.
Implement - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in AppDelegate.m, and also include #property (nonatomic, retain) UINavigationController *navControl; in AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
return YES;
}
I think it will be helpful to you.

Navigate from a view to another

The main: xxxVieController
and the second: OptionViewController
in xxxView controller I have
.h
#interface iMetroAlertViewController : UIViewController {
IBOutlet UIButton *option;
}
-(IBAction) goToOption;
and
.m
#implementation iMetroAlertViewController
-(IBAction) goToOption{
NSLog(#"Works");
OptionViewController *dvController = [[OptionViewController alloc] initWithNibName:#"OptionView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Of course a OptionViewController.xib, .m and .h are created...
When I push on the button, nothing happens (the link is done cause I have an NSLog
I don't understand why it doesn't work
It probably doesn't work because you don't have a navigation controller, so calling self.navigationController returns nil.
First you have to create the navigation controller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
RootViewController *mainMenuVC = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}