IPHONE - How to initialize a button using editButtonItem - iphone

I want to use table edit function. I know
self.navigationItem.rightBarButtonItem = self.editButtonItem;
However I want to set a UIButton set it as editButtonItem or can do edit table function

You cant use self.editButtonItem because it is present when your class is subclass of UITableView.
But you can achieve same functionality by the following code-
use a bool variable (declare in .h file says isEdit).
-(IBAction)editTable
{
if(!isEdit)
{
isEdit=YES;
yourTableView.editing=YES;
}
else
{
isEdit=NO;
yourTableView.editing=NO;
}
}
bind this IBAction to the button. you can also change the text of the button and style also by setting property of the button.

just write your code in onclick of UIButton ,that s
-(IBAction)deletebuttonPresed:(id)sender
{
table.editing=YES;
}

Related

how to check if any of the value is already hidden in iphone app

I have two button in iPhone app I want that if button1 is hidden and when we create PDF then we may check that button1 is already hidden then does not hide it again and also does not show it back when PDF generated because when PDF is created I hide some buttons.
you can write something like below...
if(button1.hidden){
//here button1 is hidden , so you can write code accordingly...
}else{
//here button1 is not hidden , so you can write code accordingly...
}
happy coding!!!!!
You can just use the .hidden property on the button
To hide the button, if not already hidden
if(!yourButton.hidden) //Button is not hidden
{
//hide button
yourButton.hidden = YES;
//do other magic here if required
}
To change the visibility of your button, you can use
yourButton.hidden = YES; //Set button hidden, (YES or NO, NO is by default)
you can check that UIButton is Hidden or not with its isHidden Property and also Hide that UIButton with its setHidden: method.
use this bellow logic with its method
if ([button1 isHidden]) {
//Generate your PDF here because if Button is hide then this condition true and load this code
}
else{
[button1 setHidden:YES];//we hide that button here and after load your code here
}

iOS Determine button that was programmatically created

I have made a horizontal scroll view with images and buttons in it using a for loop, what i want to do now is when a button is pressed, open the image it corresponds to full screen. The issue im having is determining which button has been pushed. I am using :
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then :
-(void)buttonPressed:(UIButton *)sender {}
What can i do to fix this? Thanks
Which button has been pushed
Create a property to store a tag. In the buttonPressed method set the property to sender.tag
Now you can know which was the button who got pressed last.
The sender is the button that the user has tapped, so when you create the buttons you could use the tag property and set it to an index of an array where you hold your images. But this could be a bit unstable if you change the order or amount of images or buttons for example so be prepared to check for that.
one way to do so is to keep references of your button (with a property, an attribute in your class) and test if it is the good one in your buttonPressed method
- (void)buttonPressed:(UIButton *)sender {
if (sender == self.myButton) {
// DO YOUR WORK HERE
}
}
you can also create a method for this and only this one button
by the way it is better to say
- (IBAction)buttonPressed:(UIButton *)sender
you then can set the target of your button in the interface builder interface
set tag for each button in the for loop but.tag=i;
-(void)buttonPressed:(UIButton *)sender {
if (sender.tag==1){
//display image 1
}
else if ....
}

Replace or extend UIBarButtonItem selector

I'm trying to make a custom UIToolbar which handles rotation and custom arrangement. When it's in portrait mode some of the barbuttonitems will not be visible, so I add a "more" button from which pops up a little view with these items. My problem is that when my current orientation is portrait and when I select a visible barbuttonitem ( which is not in popup ) I want to close the popup if it's open. I want the smae behavior for the uibarbuttons in the popupview to close the popup after tap.
So I'm trying to replace somehow the UIBarButtonItem's selector with my own, in which I call the already defined actions, like this :
-(SEL)extendOriginal:(UIBarButtonItem *) uibb
{
if (popup) popup.hidden = YES;
[uibb.target performSelector:uibb.action];
// return ?? do what ??
}
But how do I replace the original selector to call this custom method with my custom UIToolbar as its target ? Or how could I "extend" the original selector with this call ? Sorry if question is lame :)
Edit: In other words, I want 2 actions with 2 separate targets to be executed when UIBarButtonItem is tapped.
Thanks!
This -(SEL)extendOriginal:(UIBarButtonItem *) uibb doesn't make any sense.
I assume your are setting the target and the action of the bar button item somewhere. There you can set any method with one argument id or UIBarButtonItem* as the selector.
Therefore try to change your code to
- (void)myMethod:(UIBarButtonItem *) uibb
{
if (popup) popup.hidden = YES;
// do cool stuff here
}
and set the target as
[[UIBarButtonItem alloc] initWithTitle: #"Blabla" style: UIBarButtonItemStylePlain target: self action: #selector(myMethod:)];
Finally, I found a way to do it, not the prettiest, but it works.
In the custom toolbar class I created in its layout method a UITapGestureRecognizer to handle taps. I've set the cancelsTouchesInView to false and in the
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
method I'm returning true only for my toolbar's subviews.
This way the original target and action of each UIBarButtonItem remains the same and the supplementary code to handle my popup is the action of the UIGestureRecognizer.
Another problem was to distinguish between the tapped items on the toolbar (the tapped view in touch.view is some undocumented view, yay), eventually I did it with some BOOL iVars.
I hope this will help somebody with the same problem.

iOS / iPhone- Hiding one button when another is clicked

I have a settings view in my app which has a couple of buttons (actually UISwitches). If the "off" setting on one of the switches is selected, I'd like to hide the second switch immediately. Can this be done?
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
write the above 2 lines in your .h file and set the outlets with XIB.
Now create a method called hideButton
-(IBAction)hideButton
{
btn1.hidden = YES;
}
in XIB attach this method with btn2.
So now when you click on btn2 it will hide btn1.
Connect the two switches as outlets. lets say switch1 & switch2.
Connect this function to the valueChanged action.
- (IBAction)mySwitch1:(id)sender {
[switch2 setHidden:!(switch1.isOn)];
}
Now when switch1 is not on then switch2 will be hidden.
Add a target to the first switch which on value change calls the instance of the second switch and hides it.
Add the target:
[switch1 addTarget:self action:#selector(switchToggled:) forControlEvents: UIControlEventValueChanged];
Calls this method:
- (void) switchToggled:(UISwitch*)switch {
if ([switch isOn]) switch2.hidden = YES;
else switch2.hidden = NO;
}
NJones if statement is more effective though.
Swift 4
Within your function do the following:
btn1.isHidden = true

Toggle UIButton on the iPhone

How can I make a button that says "Show Picture" and when it's clicked it changes to "Hide Picture". I'm new to objective C, I know how to make a button in interface builder but don't know how to switch the button's text and function. Can someone help me out?
Don't abuse the tag property. It is advise only to be used as a button identifier (for example when you have few buttons in your view you set their tags as 0, 1, 2... so can identify which button is a sender). You could set some global int i variable and change it's value accordingly.
abuse the .tag property of the button. Hook up the touch down action to this function:
-(IBAction)buttonClick:(UIButton*)sender
{
if ( sender.tag )
{
sender.tag = 0;
sender.text = #"Show Picture";
// do actions when "hide" is clicked
} else {
sender.tag = 1;
sender.text = #"Hide Picture";
// do actions when "show" is clicked
}
}
Instead of (ab)using the tag property, you could also simply toggle the button between selected and not selected, like so:
- (IBAction)myButtonAction:(id)sender
{
[sender setSelected:![sender isSelected]];
// or in Objective-C 2.0 if you're so inclined
sender.selected = !sender.selected;
}
In IB, you could then set the text for the normal and the selected state of the button directly in the inspector (or programmatically through the setTitle:forState: method).
The tricky thing with this one is that a UIButton doesn't have an "official" text properly - see the docs here:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
because it's designed to have multiple, separate sets of texts, displayed according to the button's current state; i.e., whether it's currently enabled or disabled, highlighted, etc. So there's not one, simple property you can set to make this work.
So, you want to declare your button like this, as both an action and an outlet:
from button.h:
// inside the class declaration
BOOL pictureShown ; // initializer not required, defaults to 0 (NO)
UIButton * sampleButton ;
// skip irrelevant lines here
#property (nonatomic, retain) IBOutlet UIButton * sampleButton ;
- (IBAction) doSampleButton ;
Hook both of those up in Interface Builder, and then change the text using the setTitle:forState: method (and in this case, I've specified all the states, so the title stays the same across all of them). For example:
from button.m:
#synthesize sampleButton ;
- (IBAction) doSampleButton {
if (pictureShown == YES) {
// hide the picture, and then...
[sampleButton setTitle: #"Show Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
pictureShown = NO ;
} else {
// show the picture, and then...
[sampleButton setTitle: #"Hide Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
pictureShown = YES ;
}
}
You'll also note that I've declared an instance variable, "pictureShown", in the view controller for the view with the button, to track the current "mode" of the button, and that I'm essentially using an if statement inside the button's action to determine which function is carried out according to the current mode of the button, and to toggle the button text accordingly.
(I'm using this to track the current mode of the button rather than, for example, examining the current title of the button directly, or other ways of storing state on the button, because MVC-wise, this sort of state belongs in the controller class.)
(If the function required a lot of code, I'd use messages to self, i.e.:
[self showPicture] ;
[self hidePicture] ;
to avoid having to cram it all inside doSampleButton, but that's purely stylistic, rather than technically required.