iPhone SDK -> RightBarButtonItem disappears when loading view through AppDelegate - uinavigationitem

So I have an issue with my rightbarbuttonitem disappearing. I have two ways of loading this view, first time launch it loads it after user enters in their name (from an initial view). Second time (after the app exits), it checks if the name exists in my stored database and if it does it loads the view right away. This second time is where the button does not show.
The button was set in viewDidLoad of my view originally here (and is still set here for the first load):
if (self.navigationItem.rightBarButtonItem == nil){
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(buttonPressed)];
self.navigationItem.rightBarButtonItem = addButton;
[self.navigationItem.rightBarButtonItem retain];
}
Then in the .m of my AppDelegate, I added the button to think that would resolve it on the second load:
if(success){
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *control = [[ViewController alloc] initWithNibName:#"myNib" bundle:nil];
[control retain];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:control];
[navControl retain];
[self.window setRootViewController:navControl];
if (navControl.navigationItem.rightBarButtonItem == nil){
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(buttonPressed)];
navControl.navigationItem.rightBarButtonItem = addButton;
[navControl.navigationItem.rightBarButtonItem retain];
}
//[navControl release];
[self.window makeKeyAndVisible];
return;
Here's the declaration of the addButton in the header for App Delegate and my view's header:
UIBarButtonItem *addButton;
#property (nonatomic, retain) UIBarButtonItem *addButton
Other posts say to check viewDidLoad/viewWillAppear but putting that first blurb of code in either of those does not solve the issue.

Solved it myself, since the button was never added to the nib, initializing UIViewController doesn't help. I had to initialize my view controller.
In other words replace: UIViewController *control = [[ViewController alloc] initWithNibName:#"myNib" bundle:nil]; with: MyViewControllerClassName *control = [[MyViewControllerClassName alloc] initWithNibName:#"myNib" bundle:nil]; Hope this helps others in the future, thanks for taking the time to read this if you did!

Related

Navigation with xib in iOS app

Please, help to understand the navigation. I'm working with xibs. The scheme is: https://www.dropbox.com/s/o82fxpte0hmyxcq/Scheme_Simple.jpg .
Here's my code :
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
FirstViewController *firstViewController = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#implementation FirstViewController
- (IBAction)button1Tapped:(id)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.title = #"View2";
[self.navigationController pushViewController:secondViewController animated:YES];
}
#implementation SecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
thirdViewController.title = #"View3";
if (indexPath.row == 0) {
[self.navigationController pushViewController:thirdViewController animated:YES];
[secondViewTableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
So, I have questions:
Where should I create the next view? In my code view has created in "previous" class: view2 created in FirstViewController, view3 created in SecondViewController etc. All new ViewControllers are inside the method that initiates the navigation, is it right way? I think it's wrong, but the navigation is working.
Problems with headers in the navigation bar. It turns out that the title of view2 is only displayed when moving from view1 to view2, but when going back from view3 to view2 – header disappears. I googled, tried to add self.title = #"name" to viewDidLoad, initWithNibName, viewWillAppear – none of this works.
So I've solved the problem with disappearing title of navigation bar. The problem was in my custom back button: self.navigationItem.title = #"";
It was working and title "Back" from my back button disappeared but also title of navigation bar disappeared too. The right way to make back button untitled is:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];

Display an Image in DetailViewController Sent by MasterViewController using Apple SplitView Template

i'm working on an application and i'm still a beginner with ios programing.
I'm asking for your help because i used the apple MasterDetailView template.
I'm generating a list of file stored in my application, which i display within my MasterView(TableView).
When i click on one of the files contained in my list i generate an image in relationship with this file and i would like to display it in my DetailView.
Is there a solution to do it without destroying all my application :)
Thanks for you help guys!!
This is my Delegate and how my controllers are declared:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}
Assuming it's your master controller that's responsible for creating the image.... You would normally create a property in your detail controller to hold a reference to the image. Inside the method tableView:didSelectRowAtIndexPath: of the master, you would pass that reference to the detail view controller.
(Alternatively, pass the file information instead and let the detail controller create the image.)

White screen after presenting a modal view controller

I'm getting a white screen after presenting a modal view controller. This is how I do it:
SomeViewController *controller = [[[SomeViewController alloc] initWithManagedObjectContext:managedObjectContext] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:navController animated:YES];
The navigation bar works fine, as I set it up in SomeViewController, but the view's contents are not visible, and all I see is the white background color of the root window.
The strange thing is, that this used to work, but now it doesn't for some reason.
What could be the problem?
EDIT:
This is how I create SomeViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setTitle:#"Some View"];
UIBarButtonItem *sortButton = [[[UIBarButtonItem alloc] initWithTitle:#"Sort" style:UIBarButtonItemStylePlain target:self action:#selector(sortButtonClicked:)] autorelease];
[[self navigationItem] setRightBarButtonItems:[NSArray arrayWithObjects:sortButton, [self editButtonItem], nil] animated:YES];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(dismissModalViewControllerAnimated:)] autorelease];
[[self navigationItem] setLeftBarButtonItem:backButton];
// Hack to force landscape orientation
UIViewController *controller = [[UIViewController alloc] init];
[self presentModalViewController:controller animated:NO];
[self dismissModalViewControllerAnimated:NO];
[controller release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
The white screen was caused by the xib file of the controller not being part of the target(its target membership checkbox was unchecked).
Do you happen to have some other init meted also in the SomeViewController class? Please post the whole .m file. If so, you can try to delete the initWithNibName method and check if it shows the content.
EDIT:
Another strange point is the initWithManagedObjectContext method you are using on the viewController instance. Can you explain it?
try
SomeViewController *controller = [[[SomeViewController alloc] init] autorelease];
it should work fine.

navigationBar isn't showing when I put a UINavigationController in a UIPopoverController

I'm adding a UIViewController to a UINavigationController and then setting a UIPopoverController's view to the UINavigationController. Everything is working great except that I don't get a navigationBar at the top of the popoverController. I'm creating everything like this:
QueryViewController *puvc = [[QueryViewController alloc] autorelease];
UINavigationController *nc = [[UINavigationController alloc] autorelease];
[nc pushViewController:puvc animated:YES];
self.popUp = [[[UIPopoverController alloc] initWithContentViewController:nc] autorelease];
[self.popUp presentPopoverFromBarButtonItem:[self.toolbarItems objectAtIndex:0] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popUp.delegate = self;
I've tried to set navigationBarHidden = NO and it still doesn't show up. I have this code in my viewDidLoad of my QueryViewController:
self.contentSizeForViewInPopover = CGSizeMake(500.0, 500.0);
self.title = #"Queries";
Is there something that I'm missing to display the navigationBar? I am already in a UINavigationController for my main screen, could this be part of my problem?
QueryViewController *puvc = [[QueryViewController alloc] autorelease];
UINavigationController *nc = [[UINavigationController alloc] autorelease];
Maybe your code is wrong. Where is init methods?
You have to init both your QueryViewController and UINavigationController. For the second use initWithRootViewController method.
QueryViewController *puvc = [[[QueryViewController alloc] init] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:pucv] autorelease];
//[nc pushViewController:puvc animated:YES];
If you use initWithRootViewController it's not necessary to push puvc instance.
You could try also this (I like to release memory explicity, not using autorelease).
QueryViewController *puvc = [[QueryViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:pucv];
//[nc pushViewController:puvc animated:YES];
Then at the end of your code snippet remember to release puvc and nc.
[puvc release];
[nc release];
P.S. Check the code because I've written without XCode.

How to access navigationController from popoverController?

I am trying to push a new viewController into the navigationController from a popoverController but it doesnt work for me.
This is how I call to the popoverController:
PdfDetailViewController *vc=[[PdfDetailViewController alloc] initWithNibName:#"PdfDetailViewController" bundle:nil];
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 400, 280)];
vc.contentSizeForViewInPopover = CGSizeMake(700, 390);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.popoverController presentPopoverFromRect:cell.frame inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popoverView release];
[popoverContent release];
This is my code from the popoverController:
CommentsViewController *commentsViewController = [[CommentsViewController alloc] init];
commentsViewController.index = PdfID;
[self.parentViewController.navigationController pushViewController:commentsViewController animated:YES];
[commentsViewController release];
Nothing happen
Please help me... thank you!
UIPopoverController doesn't have a NavigationController unless you add one yourself.
For example
MyViewController *myViewController =
[[MyViewController alloc]
initWithNibName:#"MuViewController"
bundle:[NSBundle mainBundle]];
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:myViewController];
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
You should start by cleaning up your code so that it makes sense. Right now you are creating a UIView called popoverView for no reason and then just throwing it away. You're just confusing yourself and it's harder to understand what you are trying to accomplish when you show meaningles code.
Once you've done that, what are you trying to accomplish? Is the problem that this line doesn't work: self.parentViewController.navigationController pushViewController:? If so, I would suggest pulling it apart and logging to make sure that parentViewController and navigationController are the objects you think they are.