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.
Related
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.
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
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 having trouble setting a custom back button. Below is the current code I'm using in the parent ViewController in viewDidLoad:
UIButton *custBackButton = [[UIButton alloc] init];
[custBackButton setBackgroundImage:[UIImage imageNamed:#"back_btn.png"] forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:custBackButton];
I can get the below code to work as a test:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Test" style:UIBarButtonItemStyleBordered target:nil action:nil];
Can anyone give me a pointer or two with what I'm doing wrong for the first example?
Thank you,
don not use self.navigationItem.backbarbutton....use self.navigationItem.leftbarbutton....it will work....and before that make self.backbarbutton=nil....to remove it, otherwise it will overlap...and this wont run ur custom button