`Multiple radio button validation in iphone sdk - iphone

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.

Related

UIAlertView-Show red if data not entered

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

Having trouble dismissing 2 UIActionSheets

I have 2 UIBarButoon in a UIToolbar. Each has an IBAction to launch their own UIActionSheets.
Right now, if I present one, then click the other UIBarButton, the other ActionSheet gets presented as well, so I have two on the screen and they are overlapping. I want one to dismiss when the other is presented, etc.
I have this code now:
- (IBAction)showFirstActionSheet:(id)sender
{
if (!self.eRXActionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"eRX Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"New eRX", #"eRX Refills", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.eRXActionSheet = sheet;
[sheet release];
[self.eRXActionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.eRXActionSheet dismissWithClickedButtonIndex:self.eRXActionSheet.cancelButtonIndex animated:YES];
self.eRXActionSheet = nil;
}
- (IBAction)showSecondActionSheet:(id)sender
{
if (!self.actionSheet)
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Action Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Help", #"Lock", #"Log Out", nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
self.actionSheet = sheet;
[sheet release];
[self.actionSheet showFromBarButtonItem:sender animated:YES];
return;
}
[self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:YES];
self.actionSheet = nil;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet == self.eRXActionSheet)
{
if (buttonIndex == 0)
{
[self erxButtonClicked];
}
else if (buttonIndex == 1)
{
[self erxRefillButtonClicked];
}
}
else
{
if (buttonIndex == 0)
{
[self helpButtonClicked];
}
else if (buttonIndex == 1)
{
[self lockButtonClicked];
}
else if (buttonIndex == 2)
{
[self logOut];
}
}
}
In your code, before creating an action sheet, your are checking if the action sheet to be displayed is not nil. Logically, this will always evaluate to true.
Rather you should check if the other action sheet is not nil. If so, dismiss it and create the new one.
-(void)buttonForActionSheet1Clicked {
if (actionSheet2 != nil) {
// dismiss it
}
// show actionSheet1
}
-(void)buttonForActionSheet2Clicked {
if (actionSheet1 != nil) {
// dismiss it
}
// show actionSheet2
}
Alternatively, you can force the user to dismiss the present action sheet with the cancel button first by disabling the other button in the toolbar.
Call dismissWithClickedButtonIndex:animated: on the actionsheet you want dismissed.
E.g. in showFirstActionSheet add this line to dismiss the other:
if(self.actionSheet) [self.actionSheet dismissWithClickedButtonIndex:<indexofCancelButton> animated:YES];

How to customize Buttons in UIActionSheet?

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/

UIActionSheet making multiple UIButton changes

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.

how to place activity indicator in alert view when clicking button of that alert view

i am using the simple alert view with two buttons names as ok,cancel.
when ever i press ok,cancel alert view quits as general.
But i need when ever i click alert view ok button with out quit alert view activity indicator
will run for 2 min on the same alert view and quit. when ever i click cancel it quits normally.
can any one please help me.
Thank u in advance.
You can modify the UI control events for the OK button, so that your own event handler is called for that button and the alert view won't be dismissed until the long-running task has finished.
In that event handler attach an activity indicator to the view and start your task asynchronously using GCD.
#import <dispatch/dispatch.h>
// ...
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Test" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK" ,nil];
for (UIView *subview in alert.subviews)
{
if ([subview isKindOfClass:[UIControl class]] && subview.tag == 2) {
UIControl* button = (UIControl*) subview;
[button addTarget:self action:#selector(buttonOKPressed:) forControlEvents:UIControlEventTouchUpInside];
[button removeTarget:alert action:nil forControlEvents:UIControlEventAllEvents];
}
}
[alert show];
[alert release];
// ...
-(void) buttonOKPressed:(UIControl*) sender {
[sender removeTarget:self action:nil forControlEvents:UIControlEventAllEvents];
UIAlertView* alert = (UIAlertView*)[sender superview];
CGRect alertFrame = alert.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(0,alertFrame.size.height, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.alpha = 0.0;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating];
[alert addSubview:activityIndicator];
[activityIndicator release];
[UIView animateWithDuration:0.3 animations:^{
alert.frame = CGRectMake(alertFrame.origin.x, alertFrame.origin.y, alertFrame.size.width, alertFrame.size.height+50);
activityIndicator.alpha = 1.0;
}];
//alert.userInteractionEnabled = NO; // uncomment this, if you want to disable all buttons (cancel button)
dispatch_async(dispatch_get_global_queue(0,0), ^{
[NSThread sleepForTimeInterval:5]; // replace this with your long-running task
dispatch_async(dispatch_get_main_queue(), ^{
if (alert && alert.visible) {
[alert dismissWithClickedButtonIndex:alert.firstOtherButtonIndex animated:YES];
}
});
});
}