iOS - Three buttons in Nav. Bar - not Tab Bar - xcode

I am building an app that sort of calls for three buttons in the top nav. bar. I don't want to do a tab bar, as there will be only one item in the tab.
In the code below, the left button (Edit) works fine. The right button - the link to cards also works fine. However the stupid Add button gives me an error that it cannot find the selector (insertNewObject).
If I take out my custom code and use the code that says "Works in a pinch", well.. it works.
I'd appreciate either knowing what I'm doing wrong, or else, another way of handling the problem at hand - linking to another part of the app.
TIA.
/*
// THIS WORKS IN A PINCH
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
// END OF WHAT WORKS
*/
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 105, 44.01)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
addButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addButton];
[addButton release];
// create a standard "add" button
// create a flip button
UIBarButtonItem* flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Cards" style:UIBarButtonItemStyleBordered
target:self action:#selector(flipToFront)];
[buttons addObject:flipButton];
[flipButton release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
UIBarButtonItem* rightButtonBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = rightButtonBar;
[rightButtonBar release];

You may be missing a colon after the selector name. I think just putting in #selector(newObject) is a reference to a method with no parameters. #selector(newObject:) may work instead.

Related

putting buttons on masterdetail template xcode

My question is, what is the most appropriate way to put my own buttons on the upper bar of the Master view in a MasterDetail template.
I would like to put my own buttons on the Top portion of the Master view in the MasterDetail template. It arrives with an edit button on the left and an add (+) button on the right. I commented out the Edit button in the .m file. I drug in my own button in its place. The first thing I noticed is that my button looks different then the + button on the right. It seems to be of uniform size. The next thing is that when I created my button, it did not have a place to specify the Event (touch up inside). Could I have somehow modified the self.editButtonItem. Surely this is a common thing to do - to replace their buttons w/ ours. thaks
Create two custom buttons and set on the navigation item..
// Set navigation left button
UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLeft setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnLeft addTarget:self action:#selector(skipImage:) forControlEvents:UIControlEventTouchUpInside];
[btnLeft setFrame:CGRectMake(0, 0, 60, 29)];
[btnLeft setTitle:#"Skip" forState:UIControlStateNormal];
[btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLeft .titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
//
// Set navigation right button
btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRight setBackgroundImage:[UIImage imageNamed:#"button_signup.png"] forState:UIControlStateNormal];
[btnRight addTarget:self action:#selector(nextImage:) forControlEvents:UIControlEventTouchUpInside];
[btnRight setFrame:CGRectMake(0, 0, 60, 29)];
[btnRight setTitle:#"Next" forState:UIControlStateNormal];
[btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnRight.titleLabel setFont:[UIFont fontWithName:#"Futura" size:15.0]];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
[self.navigationItem setRightBarButtonItem:rightBarButton];
//
This example should solve your problem: http://rogchap.com/2011/06/21/custom-navigation-bar-background-and-custom-buttons/ .. originally answered here - > Custom Navigation Bar Buttons..

How to update a bar button item in a toolbar

I have a single playback controller in my app on the navigation bar and is easily found in its implementation file. So I've tried to add a toolbar and move the playback controls to that toolbar, but I don't know how to reference it programmatically and therefore cannot update the image used to indicate if music can be paused or played.
You can do this a couple of ways, one is by setting the image of the UIBarButtonItem not to be confused with the backgroundImage of a button. You can add a bar button like so:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"camera.png"] style:UIBarButtonItemStylePlain target:self action: #selector(pressButton1:)];
The image should be a grayscale image somewhere around 30 x 30 in size much like a tab bar icon. Declare the bar button in the interface file and you can set the image like this:
[barButton1 setImage:[UIImage imageNamed:#"Chats.png"]];
The other way you can do this is by using a custom UIButton like so:
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, 64, 30);
[button1 setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState: UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"Chats.png"] forState: UIControlStateSelected];
[button1 setTitle:#"Camera" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button1 addTarget:self action:#selector(pressButton1:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
Declare button1 in the interface file and then you can change the image like so:
[button1 setSelected:TRUE];

Changing the Image for a detail disclosure button (Xcode 4.2)

I have used the following suggested routine to change the detail disclosure button image in a table view cell (in tableView cellForRowAtIndexPath)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:#"ball"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
However, the event tableView accessoryButtonTappedForRowWithIndexPath is now no longer called on clicking the button.
Has anyone any ideas as to why?
I've +1'd Fry's answer, but just to pull the code in from the other site to make SO more complete: The answer is that you have to accessoryButtonTapped.... call will not be made automatically as it would for the built-in detail-disclosure, so you have to do it yourself with the target of the button.
Translating from the link code to your example above, add this line after setImage:
[myAccessoryButton addTarget:self action:#selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
Then add this later on:
- (void)accessoryButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil) {
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
(copied verbatim from this link)
Note that this assumes that the button is created in the UITableViewController (in my case I'm creating one in a custom cell so the references are slightly different)
Explanation: the accessoryButtonTapped is a custom target method for our custom button. When the button is pressed ("TouchUpInside") then we find the indexPath of the cell at the point in which the press occurred, and invoke the method that the table view would ordinarily invoke.

Add a label to UINavigationBar in addition to the titleView

I'm using a UINavigationController to push my views. I want to add a UILabel above the title in the UINavigationBar of the root view controller.
When I use the following code, the label appears, but under the UINavigationBar:
[self.view addSubview:newLabel];
Is there a way to add the label to the UINavigationBar or to the UIView so that it appears above the navigation bar?
I know I can create a new UINavigationBar and build it with multiple controls like this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
navBar.translucent = YES;
[self.view addSubview:navBar];
[navBar release];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[navBar addSubview:navLabel];
[navLabel release];
However, doesn't that defeat the purpose of using the navigation controller?
You can refer this site, may be it help you in sorting your problem
http://iphonesdevsdk.blogspot.com/2011/04/uinavigationbar.html

UITabBarController + UINavigationController problem xcode project

I have a problem, I have created a project window based application in xcode, then I create a UITabBarController that manages two views all programmatically, the second view is a tableView and I want to see in the top a UINavigationController, I have tried a lot but I don't know how to have a UINavigationController in the second view. this is the code:
ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
//The window retain tabBarController, possiamo lasciare il nostro riferimento
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
Visuale1ViewController.h
#implementation Visuale1ViewController
- (id)init{
[super initWithNibName:#"Visuale1ViewController" bundle:nil];
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:#"Visuale 1"];
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
return [self init];
}
Visuale2ViewController.h
#implementation AnswerViewController
- (id)init{
//Call the superclass's designated initializer
/*[super initWithNibName:nil
bundle:nil];*/
[super initWithStyle:UITableViewStyleGrouped];
answers = [[NSMutableArray alloc] init];
for (int i = 0; i<10 ; i++) {
[answers addObject:[Answer DefaultAnswer]];
}
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a laber
[tbi setTitle:#"Visuale 2"];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
//All below are all methods to work the table view, and all go well, the only problem it's the UINavigationController, to manage then the detail of the table...
Now I want to know how I can put a UINavigationController in the second view. I try do this, in ProjectAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc2];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, navController, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
}
In this way I can visualize the NavigationBar, but I lost the name of the SecondTabBar. Sorry for my english, how I can do this?
Yes in the second view you have to set title as
[self.navigationItem setTitle:#"Visuale2"];
For TabBar title-
UITabBar *tabBar = [self.tabBarController tabBar];
NSArray *tabBarItems = [tabBar items];
UITabBarItem *secondTabBarItem = [tabBarItems objectAtIndex:1];
[secondTabBarItem setTitle:#"Visuale2"];
I left the code as it was and I added in init Visale2ViewController this:
UIImage *i = [UIImage imageNamed:#"Hypno.png"];
[tbi setImage:i];
and now in can see the text Visuale2 in tabBar and the image...i don't know why...
You need to set the tabBarItem property for your UINavigationController. Something like this.
UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:#"Visuale 2" image:nil tag:1];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:vc2];
navController.tabBarItem = tabItem;
After looking at it,the scenario seems to be same like me.What I faced for the first time when doing Tab+Navigation.
I am sure that there is some problem with your Tab+Navigation based application. Although it shows the Tab as well as navigation are not able to navigate the basic flow.And it is very difficult to solve your problem with such less code.
Instead of this, I had an alternate solution for the same:
Once you have a tab bar in a XIB, the easiest way to approach this is to drag a UINavigationController object over from the Library window (looks like a left nav bar button on a gold background) into the Tree View for your tab bar (the text only view, not the GUI). Place it under the tab bar, then drag your existing view controller under the tab bar controller instead of under the tab bar.
When you go to view that tab you should then see a navigation bar on the top of it... if you are loading the navigation controller from another xib, you'll modify the nav bar in the tab bar xib.
else you can below you can follow the best url for the same:
http://books.google.co.in/books?id=2yYlm_2ktFYC&pg=PA179&lpg=PA179&dq=navigation+with+the+tab+based+application+iphoneSDK&source=bl&ots=nf2YYjX5Am&sig=COpHj9wOtsDChQBglpsljSTsElw&hl=en&ei=3ZoFTeGSOI_tsgbc_Iz6CQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CDAQ6AEwBQ#v=onepage&q&f=false
http://www.youtube.com/watch?v=LBnPfAtswgw
Hope this will surely solve your problem.

Resources