I've subclassed UIActionSheet, and in the -init method, I have to add the buttons individually after calling the super init (can't pass a var_args).
Right now, it looks like this:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) {
if (firstButton) {
id buttonTitle;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
}
va_end(argList);
}
}
return self;
However, my specific use in this case has no destructive button, a cancel button, and four other buttons. When it shows up, the ordering is all off, showing up as
Button1
Cancel
Button2
Button3
Like they were simply added to the end of the list, which makes sense; however, I don't WANT it to look like this; so what do I do? Is there, in fact, any way to subclass UIActionSheet correctly and make this work?
You can just add them in your correct order, and then set the cancelButtonIndex and destructiveButtonIndex manually.
For your code example:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
if (firstButton) {
id buttonTitle;
int idx = 0;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
idx++;
}
va_end(argList);
[self addButtonWithTitle:cancel];
[self addButtonWithTitle:destroy];
self.cancelButtonIndex = idx++;
self.destructiveButtonIndex = idx++;
}
}
return self;
Aviad Ben Dov's answer is correct, however the button index counter is not needed to set the index for the destroy and cancel indices. The addButtonWithTitle: method returns the index of the newly used button so we can use that value right away like so:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
if (firstButton) {
id buttonTitle;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
}
va_end(argList);
self.cancelButtonIndex = [self addButtonWithTitle:cancel];
self.destructiveButtonIndex = [self addButtonWithTitle:destroy];
}
}
return self;
The earlier answers cause the destructive button to be placed at the bottom, which is not in accordance with the HIG, and which is also very confusing for the user. The destructive button should be at the top, the cancel on the bottom, and the others in the middle.
The following orders them correctly:
sheetView = [[UIActionSheet alloc] initWithTitle:title delegate:self
cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:firstOtherTitle, nil];
if (otherTitlesList) {
for (NSString *otherTitle; (otherTitle = va_arg(otherTitlesList, id));)
[sheetView addButtonWithTitle:otherTitle];
va_end(otherTitlesList);
}
if (cancelTitle)
sheetView.cancelButtonIndex = [sheetView addButtonWithTitle:cancelTitle];
See https://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.m for an implementation (a UIActionSheet wrapper with a block-based API).
Related
In my class I need to have 2 different (or more) actionsheets. All of the sheets go to willPresentActionSheet. In willPresentActionSheet I do things like add a datepicker. But how do I know which actionsheet called the willPresentActionSheet?
EDIT: I created the actionsheet like this:
UIActionSheet *asheet = [[UIActionSheet alloc]
initWithTitle:#"Pick a value"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Select"
, nil];
[asheet showInView:[self.view superview]];
[asheet setFrame:CGRectMake(0, 117, 320, 383)];
[asheet release];
You can set the 'tag' for the action sheets, and check the tag in willPresentActionSheet: method. Simple!
Edit:
Set the tag.
actionSheet1.tag = 100;
actionSheet2.tag = 101;
And in willPresentActionSheet: method.
if (actionSheet.tag == 100) {
// actionSheet1 is going to be presented
} else if (actionSheet.tag == 101) {
// actionSheet2 is going to be presented
}
It passes the actionsheet into the method... So if you have (declared in the header) actionView1 and actionView2 then you can do...
if([actionSheet isEqual:actionView1]) {
// do stuff for 1
} else if([actionSheet isEqual:actionView2]) {
// do stuff for 2
}
Hi
I am adding a "post comment section in my iPhone APP"
rest all things are working fine but when I tap on "postController textView" a keyboard is suppose to appear from the bottom but it is not appearing However the cursor is displaying and the text that I am passing using postController.textView.text = #"" is also displaying.
Please suggest the areas to be looked for fixing this bug.
-(void)showCommentView
{
TTPostController *postController = [[TTPostController alloc] init];
// self must implement the TTPostControllerDelegate protocol
postController.delegate = self;
self.popupViewController = postController;
// assuming self to be the current UIViewController
postController.superController = self;
postController.textView.text=#"temporary text";
[postController showInView:self.view animated:YES];
[postController release];
}
above is the code that is giving call to the Three20 PostController
below is the calling method which is unchanged...
-(IBAction)postComment:(id)sender
{
[UserManager instance]authenticateUserAndProceed:self withSelector:#selector(showCommentView)];
}
-(void)showCommentView
{
TTPostController *postController = [[TTPostController alloc] init];
// self must implement the TTPostControllerDelegate protocol
postController.delegate = self;
self.popupViewController = postController;
// assuming self to be the current UIViewController
postController.superController = self;
postController.textView.text=#"temporary text";
[postController showInView:self.view animated:YES];
[postController release];
}
changed method
-(void)authenticateUserAndProceed:(id)parent withSelector:(SEL)selector
{
theParentViewController = parent;
self.theFunctionToCall = selector;
if(userid == nil)
{
GetUserInfoViewController *guivc = [[GetUserInfoViewController alloc] init];
[parent presentModalViewController:guivc animated:YES];
guivc.delegate = self;
[guivc release];
}
else {
//////////////////// below line was replaced///////////
// 2. [theParentViewController performSelector:selector];
// with below code
UIAlertView *alert =[[UIAlertView alloc]initWith Title........
[alert show];
}
}
PROBLEM SUMMARY:
as soon as the user registered, he was not able to the kyboard for the TTPostController
CHANGE SUMMARY:
As soon as the user is registered the call to
[theParentViewController performSelector:selector];
is not sent directly but the call goes to ann alertview which inter calls it.
EFETCS SUMMARY:
the user will see a "you are registered successfully" (kind of) alertview.
tapping OK on which, he will be sent to TTPostController. (this time the keyboard is appearing)
I kept the line# 2 in the AlertViewDelegate method.
I was amazed o see it working just by not calling the line 2 directly.
this worked for me.
I welcome any new and better idea to implement the same
In simple terms I have a view with three buttons;
buttonOne
buttonTwo
checkButtonStatus
if buttonOne is clicked it sets its own selected status to yes and buttonTwo selected status to no.
Clicking buttonTwo does the opposite.
Both buttonOne and buttonTwo by default are not selected.
The third button (checkButtonStatus) should perform a check to make sure at least one of the other two has been clicked.
I have the code detailed below:
- (IBAction)setButtonOne:(id)sender {
buttonOne.selected = YES;
buttonTwo.selected = NO;
}
- (IBAction)setButtonTwo:(id)sender {
buttonOne.selected = NO;
buttonTwo.selected = YES;
}
- (IBAction)checkButtons:(id)sender {
if (buttonOne.selected = NO || buttonTwo.selected = NO) {
UIAlertView *callAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"You have not selected a button"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[callAlert show];
[callAlert release];
}
}
The error I'm getting is 'Lvalue required as left operand of assignment'.
I'm not a programmer, I'm a sysadmin who's been asked to prototype something and can't get it working. All help greatly appreciated.
You need to use '==', not a single '=' when testing equality is your -checkButtons method.
I've used a recipe from the iPhone Developer's Cookbook called ModalAlert in order to get some text from a user; however, when the alert is shown, the keyboard and buttons are frozen. Here is the code for the modal alert.
+(NSString *) textQueryWith: (NSString *)question prompt: (NSString *)prompt button1: (NSString *)button1 button2:(NSString *) button2
{
// Create alert
CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:#"\n" delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];
// Build text field
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 30.0f)];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.tag = TEXT_FIELD_TAG;
tf.placeholder = prompt;
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// Show alert and wait for it to finish displaying
[alertView show];
while (CGRectEqualToRect(alertView.bounds, CGRectZero));
// Find the center for the text field and add it
CGRect bounds = alertView.bounds;
tf.center = CGPointMake(bounds.size.width / 2.0f, bounds.size.height / 2.0f - 10.0f);
[alertView addSubview:tf];
[tf release];
// Set the field to first responder and move it into place
[madelegate performSelector:#selector(moveAlert:) withObject:alertView afterDelay: 0.7f];
// Start the run loop
CFRunLoopRun();
// Retrieve the user choices
NSUInteger index = madelegate.index;
NSString *answer = [[madelegate.text copy] autorelease];
if (index == 0) answer = nil; // assumes cancel in position 0
[alertView release];
[madelegate release];
return answer;
}
Thanks!
You should probably check whether a UITextField's userInteractionEnabled property defaults to YES or NO.
// Put the modal alert inside a new thread. This happened to me before, and this is how i fixed it.
- (void)SomeMethod {
[NSThread detachNewThreadSelector:#selector(CheckCurrentPuzzle) toTarget:self withObject:nil]; }
-(void) CheckCurrentPuzzle {
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
// code that should be run in the new thread goes here
if ([gameBoard AreAllCellsFilled]) {
if ([gameBoard FilledWithoutWin]) {
//only show this message once per puzzle
if (![currentPuzzle showedRemovalMessage]) {
NSArray *buttons = [NSArray arrayWithObject:#"Yes"];
if ([ModalAlert ask:#"blah blah blah" withTitle:#"Incomplete Puzzle" withCancel:#"No" withButtons:buttons] == 1) {
NSLog(#"Remove The Incorrect Cells");
[gameBoard RemoveIncorrect];
} else {
[gameSounds.bloop2 play];
}
}
} else {
if ([gameBoard IsBoardComplete]) {
[self performSelectorOnMainThread:#selector(WINNER) withObject:nil waitUntilDone:false];
}
}
}
[pool2 release];
}
-(void) WINNER {
//ladies and gentleman we have a winner
}
I had a problem similar to this in my educational game QPlus. It bugged me because I had the "exact" same code in two related apps, and they did not have the bug. It turned out that the bug was because the selector method was not declared in the header file. I am working in Xcode 4.2.
Details below:
In .m:
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(emailLabelPressed)];
tapRecognizer.numberOfTapsRequired = 1;
[aLabel addGestureRecognizer:tapRecognizer];
[aLabel setUserInteractionEnabled:YES];
And later in the .m:
(void)emailLabelPressed {
//details
}
That works just fine in the simulator, but on an actual device the email interface presented modally will not edit. You can send or save as draft but no editing.
Then add this to the .h file:
(void)emailLabelPressed;
And voila, it works on the device. Of course this was the difference with the related apps - they both had the method declared in the header file. I would classify this as an iOS bug, but being such a novice developer I wouldn't presume to know.
Based on this, you may want to verify that your selector method moveAlert: is declared in your header file.
Enjoy,
Damien
Why isn't my UILabel being changed? I am using the following code, and nothing is happening:
- (void)awakeFromNib {
percentCorrect.adjustsFontSizeToFitWidth;
percentCorrect.numberOfLines = 3;
percentCorrect.minimumFontSize = 100;
}
Here is my Implemintation code:
- (void) updateScore {
double percentScore = 100.0 * varRight / (varWrong + varRight);
percentCorrect.text = [NSString stringWithFormat:#"%.2f%%", percentScore];
}
- (void)viewDidLoad {
percentCorrect.adjustsFontSizeToFitWidth = YES;
percentCorrect.numberOfLines = 3;
percentCorrect.minimumFontSize = 100;
percentCorrect.text = #"sesd";
}
- (void)correctAns {
numberRight.text = [NSString stringWithFormat:#"%i Correct", varRight];
}
-(void)wrongAns {
numberWrong.text = [NSString stringWithFormat:#"%i Incorrect", varWrong];
}
#pragma mark Reset Methods
- (IBAction)reset:(id)sender; {
NSString *message = #"Are you sure you would like to reset?";
self.wouldYouLikeToReset = [[UIAlertView alloc] initWithTitle:#"Reset?" message:message delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[wouldYouLikeToReset addButtonWithTitle:#"Continue"];
[self.wouldYouLikeToReset show];
// Now goes to (void)alertView and see's what is being pressed!
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSLog(#"Cancel button pressed");
}
else
{
varRight = 0;
varWrong = 0;
[self wrongAns];
[self correctAns];
percentCorrect.text = [NSString stringWithFormat:#"0.0%%"];
}
}
#pragma mark Button Action Methods
- (IBAction)right:(id)sender; {
varRight++;
[self correctAns];
[self updateScore];
}
- (IBAction)wrong:(id)sender; {
varWrong++;
[self wrongAns];
[self updateScore];
}
- (IBAction)subOneRight:(id)sender {
if (varRight > 0 ) {
varRight--;
[self correctAns];
[self updateScore];
}
}
- (IBAction)subOneWrong:(id)sender {
if (varWrong > 0) {
varWrong--;
[self wrongAns];
[self updateScore];
}
}
-(IBAction)addHalfCredit:(id)sender;
{
varWrong++;
varRight++;
[self wrongAns];
[self correctAns];
[self updateScore];
}
#end
Any ideas?
Thanks
In order for the adjustsFontSizeToFitWidth setting to come into effect, the numberOfLines property must be set to 1. It won't work if it's != 1.
Are awakeFromNib, viewDidLoad, viewWillAppear being called at all?
The minimumFontSize property will do nothing if the text fits in the current bounds with the current font. Did you set the font property for the label?
percentCorrect.font = [UIFont systemFontOfSize:20];
Finally, isn't a minimumFontSize = 100 a little too big for a min font size?
Make sure everything is hooked up correctly. Make sure the IBOutlet for the UITextfield is setup and set break points within the method and see that the code is being touched. If it is, it's possible percentCorrect hasn't been hooked up correctly.
You shouldn't have to init your label if it is in the nib. If you are, then you created the label twice. So who knows which one you are messaging to. As soon as you initialized the label, you leaked the first one. So the label you have on screen is NOT the one you are manipulating in code.
Try placing your code in viewDidLoad instead. It should be initialized by then.
If that doesn't work, try viewDidAppear: simply to try to debug this.
It's possible that percentCorrect hasn't yet been initialized. Is percentCorrect equal to nil when that function is called, by any chance? If so, wait until after it's properly initialized to set its properties.
What are you expecting to happen? Does the label show when your code is commented out? How is percentCorrect defined in the nib?
Have you tried:
- (void)awakeFromNib {
percentCorrect.adjustsFontSizeToFitWidth = YES;
percentCorrect.numberOfLines = 3;
percentCorrect.minimumFontSize = 100;
percentcorrent.text = #"What is the text in percentCorrect?";
}
I had the same problem. Seems that setText doesn't automatically force a redraw when the change happens on a non-main thread. UI updates should always be done on the main thread to ensure responsiveness. There's another way to force it, using a selector:
label = [[UILabel alloc] init]; //assumes label is a data member of some class
...
(some later method where you want to update the label)
...
[label performSelectorOnMainThread:#selector(setText) withObject:#"New label value" waitUntilDone:false];
You may also get results from simply saying:
[label setNeedsDisplay];
which will force the update internally, but at the SDK's discretion. I found that didn't work for me, thus why I recommend the selector on the main thread.
What I found is sometimes , don't rely too much on IB , just add a line to set the frame :
labelx.frame=CGRectMake(labelx.frame.origin.x,labelx.frame.origin.y, 300, labelx.frame.size.height);
Then , autoresize works !