ios UIBarButtonItem with UIBarButtonSystemItemCompose showing red button - ios

I am trying to add a UIBarButtonItem with the style of UIBarButtonSystemItemCompose. According to the apple docs it should display a compose icon that consists of a square outline. When I use the following code it just displays a red button. Does the icon only work if the uibarbuttonitem if placed inside of a UIToolBar and not a navigation bar.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:nil
style:UIBarButtonSystemItemCompose
target:self
action:#selector(tweetPressed:)] autorelease];

You are creating the button incorrectly. You need to use the proper init... method.
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(tweetPressed:)];
self.navigationItem.rightBarButtonItem = btn;
[btn release];
Look at the docs for the init... method you used. Look at what type should be passed for the style parameter and look at what the valid values are.

Related

Objective-C add UIBarButtonItem to UIViewController

I am trying to add an UIBarButtonItem and an UIViewController with the following code:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"back", #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
But when I run my app, the UIBarButtonItem does not appear. Please Help.
Before you localize your string, make sure it works with a regular string. So then you know if the code for the button is incorrect or the if you didn't set up your localized string properly.
You need to embed your view controller into navigation controller at that time so can find the UIBarButtonItem. Because it is navigation item you need to embed inside the navigation controller.

iOS: Can't minimize the keyboard after user types in TextView (xcode)

Another user provided me with the following code in order to make a mini toolbar above the keyboard when a user is typing in a specific TextView. All I need is a little "close" button to minimize the keyboard when the user is finished, so he/she can continue manipulating the app.
-(void)viewDidLoad
{
[super viewDidLoad]
UIToolbar *inputAccessoryView = [[UIToolbar alloc]init]; // Create one input accessory view, tool bar will be easy for you
inputAccessoryView.frame = CGRectMake(0,self.view.frame.size.height - 44, self.view.frame.size.width, 44);
// Add required buttons
UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:#"Font"
style:UIBarButtonItemStyleBordered
target:self action:#selector(changeFont:)];
UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:#"Remove"
style:UIBarButtonItemStyleBordered
target:self action:#selector(removeTextView:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self action:#selector(dismissKeyBoard:)];
NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil];
[inputAccessoryView setItems:items animated:YES];
//You should create an outlet for the text view before doing this
[self.questionsTextView setInputAccessoryView:inputAccessoryView];
}
However, the app crashes whenever the "close" button is pressed. I'm not sure if I am following the direction correctly: I am supposed to make an outlet for the TextView, so I made the output to the viewController's header file and named it questionsTextView. Then I synthesized it in my .m file, and named the instance _questionsTextView.
Can anyone please give me hand? I'm brand-new to xcode and am slowly getting the hang of objective-c! Thanks.
the error message you supplied suggest there something wrong with your methods.
#selector(changeFont:)]
#selector(removeTextView:)];
#selector(dismissKeyBoard:)];
double check the all three methods take a parameter as : symbol specify it should, and also check your method spelling.
verify object can respond to method:
NSLog(#"Method does respond: %d", [self respondsToSelector:#selector(aMethod)]);

Tried to add custom UIBarButtonItem to UIToolBar, but item comes out messed up

I'm trying to add a custom button (shape and color) to my UIToolBar, but it comes out much differently than it should.
What button looks like:
What it looks like in the bar:
Here's the code I used to add it:
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:#selector(backButtonTapped)];
[toolBarItems addObject:backButton];
What exactly am I doing wrong here?
Try using initWithCustomView: instead of the other initialization method
So you should first create a UIButton with the custom image and selector you want and then use this piece of code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
[toolBarItems addObject:backButton];

Setting custom UINavigationBar images for left and right buttons

This is what I have on my storyboard:
And I wanna change both buttons images. I have a 13x46 image but I'm having problems using it as the button's images. I've searched around for some code but I wasn't very successful with them.
Any ideas?
This is pretty simple stuff, just alloc/initWithImage a couple of bar button items, then apply them.
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myImage"] style:UIBarButtonItemStyleBordered target:self action:#selector(someMethod)];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"myImage"] style:UIBarButtonItemStyleBordered target:self action:#selector(someMethod)];
[[self navigationItem] setLeftBarButtonItem:leftItem];
[[self navigationItem] setRightBarButtonItem:rightItem];
This of course assumes that you are actually using a navigation controller. If you just have a navigation bar that you dragged and dropped in interface builder, then you need to make an IBOutlet for the navbar, link it up and then use something like this:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
[item setRightBarButtonItem:rightItem];
[myNavBar pushNavigationItem:item animated:NO];
You may need to add a flexible space in between.

How to give barbuttonitem action?

I want to bring up the .nib of "TableViewController" when a done button is clicked on my UIToolBar. But the below isn't allowing the click to bring up a new view. How do I rectify this? Please show me where I went wrong and what should be replaced and why.
//Here's the selector in my overlay.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)];
//Here's how I made my action. Btw, the uitoolbar has no nib, it's an overlay on the
//(camera mode).
-(void)doneButtonPressed {
TableViewController *tableView = [[TableViewController alloc]
initWithNibName:#"TableViewController" bundle:nil];
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tableView animated:YES];
}
//Yet nothing happens when I click on my done button on my overlay. And I've made sure
// i've imported .h frameworks correctly too.
Suppose you were to bring up a nib from a barbuttonitem which is on a UItoolbar overlay. How would you do it?
I was told that to make it function properly I would have to add [barButtonItem addTarget:self action:#selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside]; .
But if I add it I get this:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone addTarget:self action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
Which results in me getting an error reading "instance method' - initWithBarButtonSystemItem:target:action:forControlEvents:' not found (return type defaults to 'id')"
Instead of showing me only the correct additive, please show me the solution in addition to the code I've written here.
If you are using XCode 4, you can simply Ctrl+Drag the BarButtonItem, to your .h file, and you can create an IB Action from there automatically.
A UIBarButtonItem follows the default UIControlEventTouchUpInside and you cannot set it. Xcode should auto-suggest the methods for allocating it, but the correct code is:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)];
Note, there's no forControlEvents:.
Try these changes:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
-(void)doneButtonPressed:(id)sender
{
}
the target is passed the object that initiated the action (i.e. the UIBarButtonItem itself)
Secondly, set a breakpoint in your doneButtonPressed function. If, for example, the tableView is getting set to nil, then you'll see nothing happen. i.e. perhaps there is an issue with instantiating this view controller.

Resources