I have 6 UIbuttons on a view. I am trying to call a UIActionSheet for each button, and change the buttons image and a UIlabel. I can get the first one to work as desired. I can't seem to get the second to make the changes like the first.
Here is my code. please help.
-(IBAction)lifeStatus:(id)sender {
UIActionSheet *lifeStatus = [[UIActionSheet alloc] initWithTitle:#"Please Select" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"UNSECURED" otherButtonTitles:#"SECURED", #"PENDING", nil];
lifeStatus.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[lifeStatus showInView:self.view.window];
[lifeStatus release];
}
-(void)actionSheet:(UIActionSheet *)lifeStatus clickedButtonAtIndex:(NSInteger) buttonIndex {
if (buttonIndex == 0) {
self.lifeLabel.text = #"UNSECURED";
[self.lifeButton setImage:[UIImage imageNamed:#"RedButton.png"] forState:UIButtonTypeCustom];
self.lifeLabel.textColor = [UIColor whiteColor];
} else if (buttonIndex == 1) {
self.lifeLabel.text = #"SECURED";
[self.lifeButton setImage:[UIImage imageNamed:#"GreenButton.png"] forState:UIButtonTypeCustom];
self.lifeLabel.textColor = [UIColor whiteColor];
} else if (buttonIndex == 2) {
self.lifeLabel.text = #"PENDING";
[self.lifeButton setImage:[UIImage imageNamed:#"YellowButton.png"] forState:UIButtonTypeCustom];
self.lifeLabel.textColor = [UIColor blackColor];
}
}
-(IBAction)sceneStatus:(id)sender {
UIActionSheet *sceneStatus = [[UIActionSheet alloc] initWithTitle:#"Please Select" delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"UNSECURED" otherButtonTitles:#"SECURED", #"PENDING", nil];
sceneStatus.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[sceneStatus showInView:self.view.window];
[sceneStatus release];
}
-(void)actionSheet1:(UIActionSheet *)sceneStatus clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.sceneLabel.text = #"UNSECURED";
[self.sceneButton setImage:[UIImage imageNamed:#"RedButton.png"] forState:UIButtonTypeCustom];
self.sceneLabel.textColor = [UIColor whiteColor];
} else if (buttonIndex == 1) {
self.sceneLabel.text = #"SECURED";
[self.sceneButton setImage:[UIImage imageNamed:#"GreenButton.png"] forState:UIButtonTypeCustom];
self.sceneLabel.textColor = [UIColor whiteColor];
} else if (buttonIndex == 2) {
self.sceneLabel.text = #"PENDING";
[self.sceneButton setImage:[UIImage imageNamed:#"YellowButton.png"] forState:UIButtonTypeCustom];
self.sceneLabel.textColor = [UIColor blackColor];
}
}
Create UIActionSheet with Default Buttons. On every button tap set the button titles you need to show in actionSheet.
for (int i = 0; i < itemCount; i++) {
[actionSheet addButtonWithTitle:itemText];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
Given the formatting, your code is a bit difficult to read but if I understand you correctly, you could try the following.
First, create a selectedButton property or field. Add a target for each button:
[button1 addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchDownOutside];
[button2 addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchDownOutside];
... other buttons ...
-(void)buttonTouched:(id)sender {
self.selectedButton = (UIButton *)sender;
//Show UIActionSheet here
...
}
Then inside your UIActionSheet clickedButtonAtIndex: method, the selectedButton will be pointing towards the correct button, so it should be pretty simple to modify it as needed.
Related
I have a save button, which on click shows an alert box.I want the user to enter some data in the box and if he/she does not enter any data and press ok I want to make the alert box text field red i.e indicating its mandatory.For the moment I am showing another alert box.Please help me,Thanks.
following is the code.
-(void) saveCalculaterData{
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:REFERENCE_ID
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
textField.tag=REFERENCE_FIELD;
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:ADD_REFERENCE_ID];
[prompt addSubview:textField];
[prompt show];
[textField becomeFirstResponder];
textField.delegate=self;
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex > 0) {
NSString *textValue = textField.text;
if ([textField.text length] <= 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"No Reference"
message: #"msg"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil,nil];
[alert show];
}
else{
CalculatorData *calcData = [self getCalcData];
calcData.dataKey = textValue;
if([database saveCalculatorData:(CalculatorData *)calcData tableName:[database Table]]){
[Utils showAlert:RECORD_SAVED];
}
}
}
}
What you can do is just find the instance of the textField of the alertView with below code
UITextField *alertTextField = (UITextField*)[alertView viewWithTag:REFERENCE_FIELD];
And then just put the code to show it's color to red with this code
[alertTextField setBackgroundColor:[UIColor redColor]];
May this will solve your problem :)
Did you try this?
textField.textColor = [UIColor redColor];
Try this:
- (BOOL) alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField1 = [alertView textFieldAtIndex:0];
[textField1 setDelegate:self];
NSString *inputText = [[alertView textFieldAtIndex:0]text];
if ([inputText length] == 7) // If it's 7 chars long, enable the button.
{
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet]; // Only allow numbers
if ([inputText rangeOfCharacterFromSet:set].location != NSNotFound) { //Not a Number?
//show alert view here
textField1.backgroundColor = [UIColor redColor];
alertView.message = #"Only enter numbers please!";
return NO;
}
alertView.message = #"Fill in your number:";
return YES;
} else {
alertView.message = #"Fill in your number:";
textField1.backgroundColor = [UIColor clearColor];
return NO;
}
}
You can adapt it to your needs
I want to do validationin Multiple dynamicRadio buttons. I want to checkRadio buttonisselected` or not. if not selected then show alert and selected then go to another thing.
following code i use..
-(IBAction)btnNextClicked:(id)sender{
if ([appDelegate.questions count]> i) {
for (UIButton *btn in self.view.subviews)
{
if ([btn isKindOfClass:[UIButton class]])
{
if (btn.selected)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"BizVibe" message:#"Please select answer" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
}
else{
for (int k=0; k<[[[appDelegate.questions objectAtIndex:i-1] objectForKey:[[appDelegate.questions objectAtIndex:i-1] objectForKey:#"question"]] count]; k++) {
for (UILabel *lbl in self.view.subviews)
{
if ([lbl isKindOfClass:[UILabel class]])
{
if (lbl.tag==k)
{
[lbl removeFromSuperview];
}
}
}
for (UIButton *btn in self.view.subviews)
{
if ([btn isKindOfClass:[UIButton class]])
{
if (btn.tag==k)
{
[btn removeFromSuperview];
}
}
}
}
[self LoadQuestionAnswer];
return;
}
}
}
}
else{
Term_para *termMSP = [[Term_para alloc]init];
[self.navigationController pushViewController:termMSP animated:YES];
[termMSP release];
}
}
Please help me what i can do?
Thanks in advance..
Well I cant help in your code because i dont know what do you want to do in that code body but you can handle a button selected and UnSelected using a BOOL variable and its up to your logic. See below example -
BOOL isSelected; // Declared it globally
-(IBAction)btnClicked:(id)sender
{
if(isSelected)
{
[btn setTitle:#"Unselected" forState:UIControlStateNormal];
}
else
{
[btn setTitle:#"Selected" forState:UIControlStateNormal];
}
isSelected = !isSelected;
}
Here is a tutorial on radio button feature in iOS. Check it out. It might guide you to the right direction.
I Follow the comment and try to fix my problem,but still not working.
when i run the test demo on the simulator,i get this:
run the demo project view
and i click the test2, i want to change button title before clear the button title, but i
get this :
after i click test2 button
i can not clear the button title when click another button.
anyone can help ??
Here is my code
-(void)addbutton
{
for (int i=0; i<3; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
button.titleLabel.text = #"";
[button setTag:i];
[button setTitle:[self.demoDataArray objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:#selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.demoView addSubview:button];
}
}
-(IBAction)setButtonTitle:(id)sender
{
if ([sender tag] == 0)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test5", #"test6", #"test7", #"test8", nil];
[self addbutton];
}
else if([sender tag] == 1)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test9", #"test10", #"test11", #"test12", nil];
[self addbutton];
}
else if([sender tag] == 3)
{
self.demoDataArray = [[NSArray alloc]initWithObjects:#"test13", #"test14", #"test15", #"test16", nil];
[self addbutton];
}
}
Every time any button is pressed, you instantiate new buttons and add them on top of the other ones. You probably want to just update the existing buttons. Try this for your -addButton method instead:
-(void)addbutton
{
for (int i=0;i<3;i++)
{
NSInteger tag = i+1;
UIButton *button = (UIButton *)[self.view viewWithTag:tag];
if (!button)
{
button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
[button addTarget:self action:#selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:tag];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[self.demoView addSubview:button];
}
button.titleLabel.text = #""; // Not actually sure you need this...
NSString *titleText = nil;
if (i < self.demoDataArray.count)
titleText = [self.demoDataArray objectAtIndex:i];
[button setTitle:titleText forState:UIControlStateNormal];
}
}
Now the button is instantiated, given a target and action, tagged, and added into the button hierarchy only when it doesn't already exist. Every time a button is tapped after that, only the titles will be updated.
Also, very important:: in order for -viewWithTag: to work, you have to use a tag for your buttons that is not equal to 0, the default tag - otherwise the button's superview will be returned. This means you'll need to make the following changes in your button handler, which already had a bug with checking the tags (checking against 3 rather than 2). Increment the tags to start at 1 and end with 3 like so:
-(IBAction)setButtonTitle:(id)sender
{
if ([sender tag] == 1)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test5",#"test6",#"test7",#"test8", nil];
[self addbutton];
}
else if([sender tag] == 2)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test9",#"test10",#"test11",#"test12", nil];
[self addbutton];
}
else if([sender tag] == 3)
{
self.demoDataArray = [[NSArray alloc]initWithObjects:#"test13",#"test14",#"test15",#"test16", nil];
[self addbutton];
}
}
That method could use some cleanup as well to eliminate duplicated code and it's probably more appropriate to use a switch statement here anyways. But that's neither here nor there.
OK, this should probably get you up and running. I didn't test or compile this so if you do have some problem with the compiler that you're not able to work through on your own please let me know and I'll see if I can spot the issue.
try this
- (IBAction)firstbtn:(UIButton *)sender {
[secondbtn setTitle:#"" forState:UIControlStateNormal];
}
I want to change the image of the buttons of UIActionSheet, I have images for each button, and I want each button show the image I want. I searched the web I found a lot of answers but they didn't work.
Here is the initialization of the UIActionSheet
-(IBAction)showActionSheet:(id)sender {
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"title" delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:nil otherButtonTitles:#"Other Button 1", #"Other Button 2", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery showInView:self.view];
[popupQuery release];
}
I create a subclass CustomActionSheet derived UIActionSheet, and implement a method called customizeGUI.
I use CustomActionSheet like UIActionSheet, except I must call method customizeGUI of subclass in willPresentActionSheet delegate:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
if ([actionSheet isKindOfClass:[CustomActionSheet class]]) {
CustomActionSheet *sheet = (CustomActionSheet*)actionSheet;
[sheet customizeGUI];
}
}
(CustomActionSheet.m)
- (void)customizeGUI {
UIImageView *imgvwBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg_actionsheet.png"]];
CGRect rect = self.bounds;
imgvwBackground.frame = rect;
[self insertSubview:imgvwBackground atIndex:0];
[imgvwBackground release];
int buttonIndex = 0;
UIImage *imgButtonDestructive = [UIImage imageNamed:#"btn_actionsheet_destructive.png"];
UIImage *imgButtonCancel = [UIImage imageNamed:#"btn_actionsheet_cancel.png"];
UIImage *imgButtonNormal = [UIImage imageNamed:#"btn_actionsheet_normal.png"];
for (UIView *sub in self.subviews) {
NSString *className = [NSString stringWithFormat:#"%#", [sub class]];
if ( ([className isEqualToString:#"UIThreePartButton"]) //iOS 4
|| ([className isEqualToString:#"UIAlertButton"]) ) { //iOS 5
rect = sub.frame;
sub.hidden = YES;
UIButton *btn = [[UIButton alloc] initWithFrame:rect];
UIImage *imgForButton = nil;
if (buttonIndex == self.cancelButtonIndex) {
imgForButton = imgButtonCancel;
}
else if (buttonIndex == self.destructiveButtonIndex) {
imgForButton = imgButtonDestructive;
}
else {
imgForButton = imgButtonNormal;
}
NSString *sTitle = [self buttonTitleAtIndex:buttonIndex];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
[btn setBackgroundImage:imgForButton forState:UIControlStateNormal];
[btn setTitle:sTitle forState:UIControlStateNormal];
btn.tag = buttonIndex;
[btn addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[btn release];
buttonIndex++;
}
}
}
- (void)buttonClicked:(id)sender {
UIButton *btn = sender;
if ([self.delegate respondsToSelector:#selector(actionSheet:clickedButtonAtIndex:)]) {
[self.delegate actionSheet:self clickedButtonAtIndex:btn.tag];
}
[self dismissWithClickedButtonIndex:btn.tag animated:YES];
}
Hope this way can help you and give you an idea to customize UIActionSheet
If you change these sizes, not only might you make your application less usable, but that may violate Apple's Human Interface Guidelines, leading to a rejection of your application when submitted.
Here is the example how to use UiActionsheet by Apple
Looks like you'll just have to create your own class. I'd look into subclassing either UIView or UIActionSheet. There are those PSD things around that you can get the images you'll need, pending copyright.
EDIT: If you do end up creating your own class, and its any good, consider uploading it to http://cocoacontrols.com/
So I dynamically create 3 UIButtons (for now), with this loop:
NSMutableArray *sites = [[NSMutableArray alloc] init];
NSString *one = #"Constution Center";
NSString *two = #"Franklin Court";
NSString *three = #"Presidents House";
[sites addObject: one];
[one release];
[sites addObject: two];
[two release];
[sites addObject: three];
[three release];
NSString *element;
int j = 0;
for (element in sites)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//setframe (where on screen)
//separation is 15px past the width (45-30)
button.frame = CGRectMake(a, b + (j*45), c, d);
[button setTitle:element forState:UIControlStateNormal];
button.backgroundColor = [SiteOneController myColor1];
[button addTarget:self action:#selector(showCCView:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:j];
[self.view addSubview: button];
j++;
}
The #Selector method is here:
- (void) showCCView:(id) sender {
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSString* myNewString = [NSString stringWithFormat:#"%d", whichButton];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor whiteColor];
UINavigationBar *cc = [SiteOneController myNavBar1:#"Constitution Center Content"];
UINavigationBar *fc = [SiteOneController myNavBar1:#"Franklin Court Content"];
UINavigationBar *ph = [SiteOneController myNavBar1:#"Presidents House Content"];
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton = 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton = 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
}
Now, it is printing the correct button tag to NSLog, as shown in the method, however EVERY SINGLE BUTTON is displaying a navigation bar with "Franklin Court" as the title, EVERY SINGLE ONE, even though when I click button 0, it says "Button 0 clicked" in the console, but still performs the else if (whichButton = 1) code.
Am I missing something here?
You're using the = operator instead of the == operator in your conditions, which makes an assignment instead of a comparison.
Since the returned value of an assignment is the assigned value, first whichButton becomes 0 and that evaluates to false, and then whichButton becomes 1 and that evaluates to true, and no matter what you do you end up with the Franklin Court option.
You have a problem where you should have used == instead of =.
Instead of this:
if (whichButton = 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
...
Try this:
if (whichButton == 0)
{
NSLog(myNewString);
[self.view addSubview:cc];
}
else if (whichButton == 1)
{
NSLog(myNewString);
[self.view addSubview:fc];
}
else if (whichButton == 2)
{
NSLog(myNewString);
[self.view addSubview:ph];
}
Or this:
NSLog (myNewString); //occurs in all cases.
switch (whichButton)
{
case 0:
[self.view addSubview:cc];
break;
case 1:
[self.view addSubview:fc];
break;
case 2:
[self.view addSubview:ph];
break;
default:
// optionally handle the case where the button's tag was not 0, 1, or 2.
}