UIActionSheet Customization - iphone

Can we change the order in which the destructive button and Other buttons appear in an UIActionSheet. By default the destructive button (red colored) appears above other buttons, in my app I would like the other buttons to appear above the destructive button.

No Problem.
Just alloc and init a new UIActionSheet instance and add the buttons in your order (one after another) using –addButtonWithTitle:. This method returns you the index at which the button has been added to. You can then set the Index of the destructive button via -setDestructiveButtonIndex:.

You can make any button in the UIActionSheet as destruction button by using the UIActionSheet property destructiveButtonIndex like,
actionSheet.destructiveButtonIndex = 1;

Related

Custom UIBarButtonItem highlight issue

I have a toolbar with 5 buttons.
4 of them are regular bar button items and one of them is a custom one (A 'UIButton' inside a 'UIBarButtonItem').
I noticed that when I click between the regular buttons (not exactly on them), one of them (the closest one) still recieves the click event and is being highlighted (which is what I want).
But the custom bar button item does not show this behaviour.
When I tap between it and one of the regular buttons neither of the 2 receives the touch event. This probably because the UIButton is the one the gets the click event. Is there a way to add a touch event the containing bar button item as well? Or perhaps another way to solve this?
Thanks!
button.userInteractionEnabled = YES; I believe is the answer.
One solution might be that you create image of same as bar button item and assign to UIButton as background image, this will solve your issue.
Hope this helps you....
Solved it!
I added another UIView to the UIBarButtonItem and then I added the UIButton to the UIView.
I added a touch gesture to the UIView (And also kept the original touch event of the UIButton) that expanded the area that you can touch the button which solved the problem.

how to drag and drop UIButton

I want to drag and drop UIbutton in my app like iPhone main screen icons.
I can do this but while clicking the button it should perform an action. But this action is also called while dragging the button. I am using UIControlEventTouchDown for touch action and UIControlEventTouchDragInside for dragging the button.
Maybe you can use UIControlEventTouchUpInside for this action instead touch down.
In UIControlEventTouchDragInside you may change the shared variable named like "dragPerformed" to YES, and then, if it is NO perform click button action in UIControlEventTouchUpInside, if YES - do nothing (that is drag) and turn it to NO

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

Change (not init) a UIBarButtonItem identifier programmatically?

In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).
Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.
I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?
The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.
Any thoughts?
Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of UIBarButtonItem they use a custom UIButton and toggle custom images.
So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause UIBarButtonItem buttons then I think it's pretty safe to say there's no way to programatically change the identifier of a UIBarButtonItem.
This is what they (Apple) do to toggle the images when the button is pressed:
// Call this when the button you want to toggle is pressed:
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
Replace p.playing with whatever BOOL you want to hold the state of your button. playButton is the custom UIButton in the toolbar. pauseBtnBG and playBtnBG are the images to toggle.
This seems to work fairly well:
UIBarButtonItem *oldButton = [myToolBar.items objectAtIndex:1];
[myToolBar setItems:[NSArray arrayWithObjects:[myToolBar objectAtIndex:0], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:#selector(tapStopRefreshButton:)],nil] animated:NO];
[oldButton release];
In this example I had a toolbar for a UIWebView and when someone clicked Reload I wanted it to change to Stop. The toolbar only had a flexible space and the one button on it - to right-align the button - so I grabbed a reference to the old button, made a new one with the same selector as the old, reset the buttons on the tab bar, and then released the original button.
Not the prettiest, but you can use all standard buttons without having to override the button class(es).
What about 2 stacked toolbars? Then you can have some system buttons in the top one, and others in the bottom one. If the play button is pressed, then just hide the top toolbar.

touch effect for Button in iphone

how can i just change the color,of a button when i set the focus on the button in iphone.
I mean to say , for example we have 5 buttons, and i am just setting the focus on each of the button. i want those buttons to be higlighted with different color.
but when press or touch up inside the utton, the navigation is made to the respective forms, that is set for that button.
you can write method, for example, let it be
- (void) changeButtonState:(UIButton*) buttonToChange;
then you can call it in your onBtnClk method for button you need. in that method you can change image for your button's UIControlStateNormal mode. and if I understand right, you can use toolbar or tabbar instead of buttons.
Guess you have already asked this issue differently here..
But have you checked the answer?? You can use any of those methods at your will!