Add action to NSRunAlertPanel's button - objective-c

How can I add action to buttons of NSRunAlertPanel. Here's how I am creating alert panel:
NSRunAlertPanel(#"App Updater",
#"Network Error! ....... ....",
#"Ok",
#"Contact Support",
nil);

I think the easiest thing to do in this case is to check the return value and then take the appropriate action:
NSInteger code = NSRunAlertPanel(...);
switch(code) {
case NSAlertDefaultReturn:
break;
case NSAlertAlternateReturn:
break;
case NSAlertOtherReturn:
break;
}

Related

Saving data entered in textfield of custom cells

I have a custom cell.In that custom cell,there is a textfield and a label.I actually have made a form using custom cell.The user will enter its name,city,state,country,dob detail in that textfield.Now ,on a click of a button I want to save all this data together in a dictionary.But I am not able to understand that how can I save data for different keys using the same textfield as it is being reused.Please help with some code in objective c.Thanks in advance!
I am giving you idea, so that you can implement it
Just take a Macro, like this #define textFieldTag 1000
You are re-using the textfField right. So, in cellForRowAtIndexPath (if u r using table view) set the tag of the textField like this: cell.textType.tag = indexPath.row + textFieldTag;
then
access the textField using delegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
switch (textField.tag - textFieldTag) {
case 0:{
NSString *name = textField.text;
//Take a NSMutableDictionary and add the values over here
break;
}
case 1:
{
NSString *city = textField.text;
break;
}
case 2:
{
NSString *state = textField.text;
break;
}
case 3:
{
NSString *country = textField.text;
break;
}
case 4:
{
NSString *dob = textField.text;
break;
}
default:
break;
}
}
And one thing, when you click the save or what ever button u r using to submit the form remember to dismiss the keyboard [textField resignFirstResponder];, otherwise the last value can be nil.
Thank you

What is causing this: Cannot jump from switch statement to this case label [duplicate]

This question already has an answer here:
Defining a block in a switch statement results in a compiler error
(1 answer)
Closed 7 years ago.
This is a switch statement that I am getting errors on:
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// show wait view here
statusLabel.text = #"Processing...";
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock iClooud Syncing
statusLabel.text = #"Done!";
NSError *error = nil;
[SFHFKeychainUtils storeUsername:#"IAPNoob01" andPassword:#"whatever" forServiceName: kStoredData updateExisting:YES error:&error];
// apply purchase action - hide lock overlay and
[oStockLock setBackgroundImage:nil forState:UIControlStateNormal];
// do other thing to enable the features
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = #"";
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(#"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
statusLabel.text = #"Purchase Error!";
break;
default:
break;
}
The last two cases, plus the default, are giving me the following error:
Cannot jump from switch statement to this case label
I have used the switch statement many, many times; this is the first time I have seen this. The code has been copied from a tutorial (here), which I am trying to adapt for my app. Would appreciate the help on this one. SD
C is not Swift. You'll be happier if you structure your switch statements using curly braces round all of the cases interiors, like this:
switch (tag) {
case 1: { // curly braces
// ...
break;
}
case 2: { // curly braces
// ...
break;
}
case 3: { // curly braces
// ...
break;
}
}
The extra level of curly braces allows you to do things you can't do otherwise.

Objective-C A switch statement inside another switch statement

So I have created a switch statement inside another switch statement for certain sections and rows, quite hard to explain so have a look at the actual code. Anyways I was wondering if such patter should be avoided or if it does not really matter, I am new to stackoverflow as you may have already noticed so I am not entirely sure this question has been asked before.
switch(indexPath.section) {
case 0: {
switch ([indexPath row]) {
case 0: {
[self.navigationController pushViewController:introductionViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
case 1: {
[self.navigationController pushViewController:matterViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
break;
}
case 1: {
break;
}
}
}
A switch statement inside of a switch statement is perfectly legal. There's nothing inherently wrong with it, though it may be difficult to follow if there are many cases in either statement. One solution is to make the inner switch statement into a separate function or method. For example, if you have this:
switch (a)
{
case foo:
switch (b)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
break;
case foo2:
...whatever
}
You could do it like this:
void handleFoo (int x)
{
switch (x)
{
case bar:
doSomething();
break;
case bas:
doSomethingElse();
break;
}
}
...
main ()
{
...
switch (a)
{
case foo:
handleFoo(b);
break;
case foo2:
...whatever;
break;
}
}

ios there is a way to combined switch cases?

I was wondering if there is a way to combined switch cases for example:
switch (value)
{
case 0,1,2:
nslog (#"0,1,2 cases");
break
case 3:
nslog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
I'll really appreciate your help
You mean, something like this?
switch (value)
{
case 0:
case 1:
case 2:
NSLog (#"0,1,2 cases");
break;
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}
You know, the switch case structure will execute each line inside the braces starting from the corresponding case line, until it reach the last one or a break. So, if you don't include a break after a case, it will go on executing the next case also.
Alternatively, you can do this...
case 0:
case 1:
case 2:
NSLog();
break;
case 3:
NSLog()
break;
default:
NSLog();
break;
It is also possible to use ranges (a little bit less code). The following example illustrates it:
switch (value)
{
case 0 ... 2:
NSLog (#"0,1,2 cases");
break
case 3:
NSLog (#"3 cases");
break;
default:
NSLog (#"anything else");
break;
}

Enumeration value 'SHKShareTypeUndefined' not handled in switch

I get the warning Enumeration value 'SHKShareTypeUndefined' not handled in switch in the below code. I bolded the relevant line and pointer:
+ (NSArray *)favoriteSharersForType:(SHKShareType)type
{
NSArray *favoriteSharers = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:#"%#%i", SHK_FAVS_PREFIX_KEY, type]];
// set defaults
if (favoriteSharers == nil)
{
switch (type)
{
case SHKShareTypeURL:
favoriteSharers = [NSArray arrayWithObjects:#"SHKTwitter",#"SHKFacebook",#"SHKReadItLater",nil];
break;
case SHKShareTypeImage:
favoriteSharers = [NSArray arrayWithObjects:#"SHKMail",#"SHKFacebook",#"SHKCopy",nil];
break;
case SHKShareTypeText:
favoriteSharers = [NSArray arrayWithObjects:#"SHKMail",#"SHKTwitter",#"SHKFacebook", nil];
break;
case SHKShareTypeFile:
favoriteSharers = [NSArray arrayWithObjects:#"SHKMail", nil];
break;
case SHKShareTypeUndefined:
break;
}
// Save defaults to prefs
[self setFavorites:favoriteSharers forType:type];
}
This warning is in ShareKit and I am not sure how to fix it.
Thanks!
Add dummy case for that enum value:
case SHKShareTypeUndefined:
break;
Or set your "Check switch statements" flag to NO in your target settings (warnings section)
You can also use a default case:
switch (type) {
case SHKShareTypeURL:
favoriteSharers = ...
break;
// ...
default:
NSLog(#"Unexpected case - will do nothing here");
break;
}
If you have reason to want neither to add cases for all values of the enum nor to add a default case, and if you're compiling with clang, you can write
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch"
switch (type) {
//...
}
#pragma clang diagnostic pop