actionSheet clickedButtonAtIndex is not work in objective c? - objective-c

I try to click button, select from action sheet and then set the selected index to the button text label's text. My button is below. But it not show selected index in button text. How can I solve this?
- (IBAction)cinsiyetBtnClick:(id)sender {
popupQuery1 = [[UIActionSheet alloc] initWithTitle:#"Cinsiyetiniz"
delegate:self
cancelButtonTitle:NO
destructiveButtonTitle:NO
otherButtonTitles:#"Bay", #"Bayan",nil];
popupQuery1.tag = 1;
// popupQuery1.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery1 showInView:[UIApplication sharedApplication].keyWindow];
[popupQuery1 release];
}
- (IBAction)medeniDurumBtnClick:(id)sender {
popupQuery1 = [[UIActionSheet alloc] initWithTitle:#"Medeni Durumunuz"
delegate:self
cancelButtonTitle:NO
destructiveButtonTitle:NO
otherButtonTitles:#"Evli", #"Bekar",#"Dul",#"Boşanmış",nil];
popupQuery1.tag = 2;
// popupQuery1.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery1 showInView:[UIApplication sharedApplication].keyWindow];
[popupQuery1 release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(actionSheet.tag==1){
if (buttonIndex == 0) {
self.cinsiyetBtn.titleLabel.text = #"Bay";
} else if (buttonIndex == 1) {
self.cinsiyetBtn.titleLabel.text = #"Bayan";
}
}
else if (actionSheet.tag == 2){
if (buttonIndex == 0) {
self.medeniDurumBtn.titleLabel.text = #"Evli";
} else if (buttonIndex == 1) {
self.medeniDurumBtn.titleLabel.text = #"Bekar";
} else if (buttonIndex == 2) {
self.medeniDurumBtn.titleLabel.text = #"Dul";
} else if (buttonIndex == 3) {
self.medeniDurumBtn.titleLabel.text = #"Boşanmış";
}
}
}

There are several states in which button could be: UIControlStateNormal, UIControlStateHighlighted, UIControlStateSelected and UIControlStateDisabled.
In most cases when you want to update title of button you should set new title for normal state:
[button setTitle:#"New Title" forState:UIControlStateNormal];
Sometimes you would need to set titles for all the states.
So now you should just replace all your self.cinsiyetBtn.titleLabel.text = #"Bay"; with something like this:
[self.cinsiyetBtn setTitle:#"Bay" forState:UIControlStateNormal];

Related

delete a character from a string Xcode

I am using the following code to add a character to a mutable string then put it into a textview from a custom keyboard
- (IBAction) press:(id)sender {
[[UIDevice currentDevice] playInputClick];
if (sender == AEE) {
self.BACK.hidden = NO;
A = #"A";
NSLog(#"sender:%#",sender);
}
if (sender == BEE) {
self.BACK.hidden = NO;
A = #"B";
}
if (sender == CEE) {
self.BACK.hidden = NO;
A = #"C";
}
if (sender == DEE) {
self.BACK.hidden = NO;
A = #"D";
}
if (sender == EEE) {
self.BACK.hidden = NO;
A = #"E";
}
if (sender == EFF) {
self.BACK.hidden = NO;
A = #"F";
}
if (sender == ONE) {
self.BACK.hidden = NO;
A = #"1";
}
if (sender == TWO) {
self.BACK.hidden = NO;
A = #"2";
}
if (sender == THREE) {
self.BACK.hidden = NO;
A = #"3";
}
if (sender == FOUR) {
self.BACK.hidden = NO;
A = #"4";
}
if (sender == FIVE) {
self.BACK.hidden = NO;
A = #"5";
}
if (sender == SIX) {
self.BACK.hidden = NO;
A = #"6";
}
if (sender == SEVEN) {
self.BACK.hidden = NO;
A = #"7";
}
if (sender == EIGHT) {
self.BACK.hidden = NO;
A = #"8";
}
if (sender == NINE) {
self.BACK.hidden = NO;
A = #"9";
}
if (sender == ZERO) {
self.BACK.hidden = NO;
A = #"0";
}
if (display.length <= 7) {
[self.display appendString:A];
DONE.hidden = YES;
}
if (display.length == 8) {
DONE.hidden = NO;
}
else {
[self.display appendString:#""];
}
//**THIS IS WHERE I DELETE CHARATERS**
if (sender == BACK) {
[display deleteCharactersInRange:NSMakeRange([display length]-2, 2)];
NSLog(#"display2:%#", display);
if (self.display.length <1) {
BACK.hidden = YES;
}
if (display.length < 8) {
DONE.hidden = YES;
}
}
you.text = display;
}
When I use the code to delete characters it works fine with for all the characters 1 - 7. Wherever I am if I hit the backspace button it deletes one character. However when I hit the backspace button to delete a character from having 8 characters it deletes two characters. If I change the code to only delete one character like
[display deleteCharactersInRange:NSMakeRange([display length]-1, 1)];
it only deletes one character once. And if I put
[display deleteCharactersInRange:NSMakeRange([display length]-1, 2)];
it does the same thing. But when I put
[display deleteCharactersInRange:NSMakeRange([display length]-2, 1)];
my app crashes and I get an out of bounds error. I have tried using if display =<7 use one the -2,2 code and if display ==8 use the -1,1 code but nothing make it reduce the number of character by one all the time. Does anyone have any suggestions?
I figured out that if I created a separate action to delete and used the following code everything seemed to work ok
(IBAction) Delete:(id)sender{
[[UIDevice currentDevice] playInputClick];
if (sender == BACK) {
if ([display length] > 0) {
[display setString:[display substringToIndex:[display length]-1]];
you.text = display;
}
else {
you.text = display;
//no characters to delete... attempting to do so will result in a crash
}
if (self.display.length <1) {
BACK.hidden = YES;
}
if (display.length < 8) {
DONE.hidden = YES;
}
}

Two UiAlertView messages with different button actions

I have been successfully created two alerts with four buttons. The first AlertView works great but the second alertView crashes my up. Here is my code:
-(void)showAlertWithTextField{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set Time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
[alertView setTag:101];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
[alertView show];
}
-(void)showexitAlertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Timeout" message:[NSString stringWithFormat:#"Your final score is: %i", runningSore] delegate:self cancelButtonTitle:#"Restart" otherButtonTitles:#"Exit", nil];
[alertView setTag:102];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if (alertView.tag == 101 && buttonIndex == 0) {
//if (buttonIndex == 0){
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1){
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
//controller.countdownLabel.text= score;
[self presentViewController:controller animated:YES completion:nil];
}
else if (alertView.tag==102 && buttonIndex == 0){
NSLog(#"Restart");
}
else if (buttonIndex == 1){
NSLog(#"Exit");
}
}
Here is the drbugging message when i press a button from the second Alert view:
2013-09-11 17:12:06.106 Diplomatiki[2808:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
* First throw call stack:
(0x1c42012 0x1737e7e 0x1c41deb 0xa7f0f0 0xb5d0 0xa72020 0x174b705 0x682920 0x6828b8 0x743671 0x743bcf 0x742d38 0x6b233f 0x6b2552 0x6903aa 0x681cf8 0x1fc2df9 0x1fc2ad0 0x1bb7bf5 0x1bb7962 0x1be8bb6 0x1be7f44 0x1be7e1b 0x1fc17e3 0x1fc1668 0x67f65c 0x29fd 0x2925 0x1)
libc++abi.dylib: terminate called throwing an exception
I will apreciate any suggestions. Thank you in advance.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
This is error, your second alertView doesn't contain any textField. As in documentation says:
Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field.
This is a solution that should work
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101 && buttonIndex == 0) {
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
...
}
Crash is due to your second alert view don't have a textfield. But you are trying to access the textfield and it's value from the clickedButtonAtIndex:regardless which alerview is clicked.
Implement like: clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101)
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if( buttonIndex == 0)
{
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1)
{
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
else if (alertView.tag==102)
{
if(buttonIndex == 0)
{
NSLog(#"Restart");
}
else if (buttonIndex == 1)
{
NSLog(#"Exit");
}
}
}
Axel, the same problem as in previous case
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag == 101){
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 ){
return YES;
}
else{
return NO;
}
}else{
return NO;
}
}

uiscrollview rest when touch

i have UIScrollView it has next button when click it run this code
-(IBAction)ScrollPage:(id)sender {
NSLog(#"ScrollPage Called");
NSInteger positionX;
if ([sender tag] == 1) {
positionX = AddNewScroll.contentOffset.x - 320;
}else {
positionX = AddNewScroll.contentOffset.x + 320;
}
[AddNewScroll setContentOffset:CGPointMake(positionX,0) animated:YES];
}
and it will move the scroll to new page , then i have button there when click it will call
-(IBAction)SelectImg:(id)sender {
UIActionSheet *imgMenu = [[UIActionSheet alloc] initWithTitle:#"choose image"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Camera"
otherButtonTitles:#"Album",nil];
imgMenu.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[imgMenu showInView:[UIApplication sharedApplication].keyWindow];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = YES;
imgPicker.delegate = self;
#try {
if (buttonIndex == 0) {
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
if (buttonIndex == 1) {
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Start iPhone PhotoLibrary
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
[self presentModalViewController:imgPicker animated:YES];
} else
//Start iPad PhotoLibrary
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad){
aPopover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[aPopover presentPopoverFromRect:CGRectMake(250, 40, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
}
the Problem is when it call the actionSheet the UIScrollView by itself go to first page contentOffset to 0 then call scrollViewDidScroll
ps. Paging Disable
In the method clickedButtonAtIndex, you are re-allocating your UIImagePickerController. Isn't that the reason why it goes back to 0?
Also, what relation is there between AddNewScroll and imgPicker?

How to check the return value of UIAlertView?

I want to check if the user click "yes" for example I want to put a certain action.
This is my line of code:
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Are you sure?" message:#"this will start a new game" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[mes show];
So I want to say if user tap "yes" preform this action
this is my method: I want to say if the user click "yes" create the new game.
- (IBAction)newGame:(UIButton *)sender {
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:#"Are you sure?" message:#"this will start a new game" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[mes show];
self.flipsCount = 0;
self.game = nil;
for (UIButton *button in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
card.unplayble = NO;
card.faceUp = NO;
button.alpha = 1;
}
self.notificationLabel.text = nil;
[self updateUI];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex])
{
//User clicked ok
NSLog(#"ok");
[self newGame:[UIButton alloc] ];
}
else
{
//User clicked cancel
NSLog(#"cancel");
}
}
it takes ButtonIndex values from left to right .
Insert the following method in the same class (or in the class you have set as delegate):
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//User clicked ok
}
else
{
//User clicked cancel
}
}
Use the delegate method of UIAlertView as
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)//Yes
{
self.flipsCount = 0;
self.game = nil;
for (UIButton *button in self.cardButtons)
{
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]];
card.unplayble = NO;
card.faceUp = NO;
button.alpha = 1;
}
self.notificationLabel.text = nil;
[self updateUI];
}
else//No
{
//do something
}
}

is it possible to open a new view as we click button in UIActionSheet (for iPhone App)?

is it possible to open a new view as we click button in UIActionSheet?
-(IBAction)showActionSheet:(id)sender {
UIActionSheet *Query = [[UIActionSheet alloc] initWithTitle:#"Title" delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:#"Destructive Button" otherButtonTitles:#"Button1", #"Button2", nil];
Query.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[Query showInView:self.view];
[Query release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
view 1
} else if (buttonIndex == 1) {
view 2
} else if (buttonIndex == 2) {
view 3
} else if (buttonIndex == 3) {
self.label.text = #"Cancel Button Clicked";
}
}
Thank you in advance
Yeah, of course. You can add your views here the same way you would add it if the user was pressing the UIButton object. (e.g. push view controller modally, or adding the required view to your view hierarchy with [self.view addSubview:viewN]