I am stuck in a small problem,
I dynamically create a UIBarButtonItem using this code..
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(didSelectPopitDownFromNavBar:)];
self.navigationItem.rightBarButtonItem = rightButton;
and the selector method is this..
-(IBAction)didSelectPopitDownFromNavBar:(id)sender event:(UIEvent *)event
so it gives a crash on click.. how can i pass this event..?
All of the parameters form part of the method signature, so your button would need to use:
#selector(didSelectPopitDownFromNavBar:event:)
but this probably isn't going to do what you want as the button will only expect to pass itself (as the sender).
If your function was
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(didSelectPopitDownFromNavBar:)];
...
-(IBAction )didSelectPopitDownFromNavBar:(id)sender
It shouldn't crash, the problem is likely the selector isn't properly defined,
see this question - Passing parameters on button action:#selector
Related
I have the following implemented:
UIImage *rightimage = [UIImage imageNamed:#"notification-new.png"];
UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithImage:rightimage style:UIBarButtonItemStylePlain target:nil action:#selector(goNotificationNow)];
self.navigationItem.rightBarButtonItem = rightbutton ;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
Basically, I want the button to push to another ViewController when it is pressed, so I set "goNotificationNow" as :
- (void)goNotificationNow
{NotificationsViewController *ok = [[NotificationsViewController alloc]init];
[self.navigationController pushViewController:ok animated:YES];
}
Now the button can be pressed but it does not do anything. I must be missing something fundamentally just can't seem to find out what it is. I tried using storyboard as well. and changing the function to:
- (void)goNotificationNow
{[self performSegueWithIdentifier:#"notifications" sender:self];}
Your target is nil. You should set it to self. The target is the object that will perform the selector call.
You need to set the target for the action, in this case you're probably wanting to change target:nil to target:self.
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.
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)]);
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];
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.