i have an error which i donno how to fix it it's making me crazy. i have searched about the solution a lot but didn't find anything which works here .
here is the code and you can see the error in the picture .
thanks
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:body]
delegate:nil
cancleButtonTitle:[NSString stringWithUTF8String:cancleLable]
otherButtonTitles:[NSString stringWithUTF8String:firstLable],
[NSString stringWithUTF8String:secondLable], nil];
[alert show];
Related
I have a simple question with no answer.
I have a UIAlertView local variable like this,
- (void)insertNewObject:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"title"
message:#"message"
delegate:nil
cancelButtonTitle:#"cancelButtonTitle"
otherButtonTitles:#"otherButtonTitle", nil];
[alert show];
}
When I execute the - (void)insertNewObject:(id)sender method, every time the reference count of the UIAlertView is increased one by one and they are not discarded.
So, any issue is on this with ARC. I need to keep the living objects count without increase while I execute the - (void)insertNewObject:(id)sender method.
I tried making a straightforward macro for creating and showing a simple "ok" dialog box in iOS:
#define ALERT_DIALOG(title,message) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
If I try to use it in my code:
ALERT_DIALOG(#"Warning", #"Message");
I get the error:
Parse Issue. Expected ']'
And the error seems to be pointing at the second # right before "Message".
However, if I simply copy paste the macro I do not get this error:
NSString *title = #"Warning";
NSString *message = #"Message";
do
{
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert_Dialog show];
} while ( 0 );
Is there something against using Objective-c constructs in macros? Or is that something else?
The problem with your macro is that both occurrences of message in
... [[UIAlertView alloc] initWithTitle:(title) message:(message) ...
are replaced by #"Message", resulting in
.... [[UIAlertView alloc] initWithTitle:(#"Warning") #"Message":(#"Message") ...
and that causes the syntax error.
I don't think that it is really worth defining this as a macro, but if you do, you have to use macro arguments which do not occur at places where they should not be expanded, e.g.
#define ALERT_DIALOG(__title__,__message__) \
do\
{\
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(__title__) message:(__message__) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];\
} while ( 0 )
or similar.
Instead of declaring it as a macro, you can declare it as a C function instead:
void ALERT_DIALOG(NSString *title, NSString *message) {
UIAlertView *alert_Dialog = [[UIAlertView alloc] initWithTitle:(title) message:(message) delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];\
[alert_Dialog show];
}
I've got a simple UIAlertView showing when user runs the app. It has this structure:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
The question is, How can I customize it to show every 5 runs for example?
Thanks in advance ;)
You can use NSUserDefaults to store app executions count for key AppRunCount (you can introduce your own key name):
int runCount = [[NSUserDefaults standardUserDefaults] integerForKey:#"AppRunCount"] + 1
[[NSUserDefaults standardUserDefaults] setInteger:runCount forKey:#"AppRunCount"];
if (runCount <= 5) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
You can just add code above to viewDidLoad
There will be a method in AppDelegate class like
- (void)applicationDidBecomeActive:(UIApplication *)application
Now create one NSUSerdefaults in this method and make one integer and increment that integer and save it in NSUserdefaults
Now every time application starts that method will be called and integer will incremented
Now make if condition in that method like below
if(your integer which has nsuserdefaults >=5)
{
your alertview
again here make your nsinteger to Zero which is stored in nsuserdefaults
your integer which has nsuserdefaults =0
}
This is the answer of your second question,Every after 5 times app run. alert will be pop up
Let me know it is working or not..!!!!
Happy Coding!!!!
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"the response string is %#",responseString);
if ([responseString isEqualToString:#"No Data Available"] )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"No data Avilable in server"delegate:self cancelButtonTitle:nil otherButtonTitles:
#"OK", nil];
[alert show];
[alert release];
}
in my connectiondidfinishloadin delegate method i have the above code. when the server replies there is no data , it does not go into the if loop for some reason. not sure why. any hint will be greatly appreciated.
Thank You.
ResponseString is probably null/nil and runtime knows that that if statement will b false because of this, so skips it. You should see this with your NSLog(#"the response string is %#",responseString);
On a side note, Charles is a good debugging tool for this- you can see the http info as it goes out and comes in.
my application is running fine in simulator...but not on real device....and i have jailbroken iphone so i am unable to debug through device...if i use try catch something like this
#try
{
Statements
}
#catch (NSException *ex) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat:#"%#",ex]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
will my application show error on alertview before crashing?
if not how could i find where is the bug?
Catching the exception means you are doing something in response to this "error" coming about and it wont crash the application , thats the point of catching exceptions-to tell how to handle cases where errors arrise so your app wont crash, so yes the alert view will show...
That will work so long as your #catch block doesn't throw any exceptions while trying to build the UIAlertView. Make sure you release or autorelease it, and support the UIAlertViewDelegate protocol.
Try this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[ex name]
message:[ex reason]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];