issue with pushing to navigation bar - iphone

I have the following code:
- (void)setupBook
{
if (self.bookNavigationController) {
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
self.myBookVC = bookVC;
[bookVC release];
myBookVC.pageTitle = #"My Book";
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:myBookVC];
self.bookNavigationController = nController;
[nController release];
}
}
and then in the other parts of the code I have:
[self.someOtherNavigationController pushViewController:self.bookVC];
however now when I try to present self.bookNavigationController as a modal view controller, it is as the myBookVC is not there. why is this? It just shows up an empty view with a navigation bar.

When to first create your view controller you are assigning it to a property, however when assign to as the nav controller's rootViewController you are referencing an ivar. Use the property instead when assigning the rootViewController.
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
bookVC.pageTitle = #"My Book";
self.myBookVC = bookVC;
[bookVC release];
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:self.myBookVC];
self.bookNavigationController = nController;
[nController release];

Related

Populate Table With Help of PopOverController - Objective C

I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.

can't present modalViewController

I tried to present a modal view controller on a view did load.
Here's the code:
if (!self.loginNavViewController_){
AHLoginViewController * loginVC = [[AHLoginViewController alloc] initWithNibName:#"AHLoginViewController" bundle:nil];
/*
AHTestViewController * test = [[AHTestViewController alloc] initWithNibName:#"AHTestViewController" bundle:nil];
*/
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginVC];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
navController.title = #"Login to Instagram";
self.loginNavViewController_ = navController;
[self presentModalViewController:self.loginNavViewController_ animated:YES];
if (self.loginNavViewController_ == nil){
NSLog(#"NIL");
} else {
NSLog(#"NOT NIL");
}
}
However I don't see a modal view controller being shown. Why??
A view controller receives viewDidLoad immediately after loading the view and before the view is inserted into a view hierarchy. In other words, it cannot present a modal view controller because its own view is not in any window yet.
Try to do that in viewWillAppear: or viewDidAppear: instead.
I Think you can use in view did load also I am already using this.
InfoViewController *infoViewController = [[InfoViewController alloc]initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:infoViewController];
[self.navigationController.view addSubview:nav.view];
you add many model on main view using view did load.
Welcome.

How to Display title for uitabbarcontroller

I have created uitabbarview controller using the below code
In the uitabbar controller i want to display tile for every view
Please let me know how to add title for each view ( tab bar item )
myTabBarController = [[UITabBarController alloc] init];
MyDialerViewController *aDialerViewController = [[MyDialerViewController alloc]init];
MyCallLogViewController *aCallLogViewController = [[MyCallLogViewController alloc] init];
TemplateViewController *aTemplateViewController = [[TemplateViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:aDialerViewController, aCallLogViewController, aTemplateViewController, nil];
myTabBarController.viewControllers = controllers;
myTabBarController.delegate = self;
myTabBarController.selectedIndex = 0;
[controllers release];
[aDialerViewController release];
[aCallLogViewController release];
[aTemplateViewController release];
[self.window addSubview:myTabBarController.view];
[self.window makeKeyAndVisible];
I set the title of the navcontroller directly in the appDelegate.
aDialerViewController.title = NSLocalizedString(#"Dialer Title", #"Dialer Title");
aCallLogViewController.title = #"Title";
aTemplateViewController.title = #"Title";
I'd also set it in the viewDidLoad method of those viewControllers.
Works fine for me. Remember to localize, just incase you need it in the future. (if not intended to, you should look into it)
self.viewcontroller.tabBarItem = // a UITabBarItem instance
Make an instance of UITabBarItem
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITabBarItem_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UITabBarItem
(id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
You could write that in the initializer function of each viewcontroller.

Right design pattern for tabbed navigation views?

I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.

data passing in navigation based application

i am working on a navigation based application
in the rootviewcontroller i hav a few UITextFields. There is a button on rootviewController, on clicking that button i am changing the view using pushviewcontroller. I want to use values entered in these UItextFields in mapview ie my 2nd view.
PLZ suggest me something.
you could pass all the information to your second controller before pushing to navigation stack.
See the pseudo code for your reference :
First:
MyMapController* myController = [MyMapController alloc] initWithValues:Value1,value2,value3,......valuen];
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
Second:
MyMapController* myController = [MyMapController alloc] init];
myController.value1 = value1;
myController.value2 = value2;
myController.value3 = value3;
............
myController.value7 = value7;
myController.value8 = value8;
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
There could be more approach for sending the data,
Before pushing assign the values to the second controller.
Say you have to send value of textfield1. Now
secondController.textfield1 = textfield1;
Then do your pushing of controller.
Use an NSDictionary object to store the values from the first controller. Say you are using two text fields – textField1 and textField2.
- (void)loadSecondController {
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:textField1.text, #"textField1", textField2.text, #"textField2", nil];
// Assuming you are using IB files
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
controller.values = values; // Declare a NSDictionary property 'values' in SecondViewController
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
Now in SecondViewController, you can access them as
...
textField1Value = [values objectForKey:#"textField1"];
textField2Value = [values objectForKey:#"textField2"];
...