use of undeclared identifier 'alertView' - objective-c

I'm using theos to build my tweak. And this happened: Tweak.x:18:10: error: use of undeclared identifier 'alertView'
Here's my code
#import "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSTask.h"
%hook SBStatusBarManager
UITextField *textField;
- (void)handleStatusBarTapWithEvent:(id)arg1
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
textField.placeholder = #"123";
[alert show];
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInterger)buttonIndex
{
if (buttonIndex == 1)
{
UITextField *command = alert.textField;
NSTask *task = [[NSTask alloc] init];
task.launchPath = #"/bin/bash";
task.arguments = #[command];
[task launch];
UIAlertView *alert = [[UIAlertView alloc] init]
initWithTitle:#"123"
message:nil delegate:self
cancelButtonTitle:#"123" otherButtonTitles:nil];
}
}
}
%end
Please help, thanks very much.

The closing brace for handleStatusBarTapWithEvent is in the wrong place. You’ve got alertView:clickedButtonAtIndex: defined inside handleStatusBarTapWithEvent, which is not correct, and the compiler is confused by that. Move the closing brace for handleStatusBarTapWithEvent before alertView:clickedButtonAtIndex:

Related

UIAlertView with textField

I experiment at the moment with some alertViews. Now try to set an AlertView with two textField`s. When I click "done" it should show a Log.
At the moment, the code looks like that:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Titel" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:#"Cancel", nil];
The TextField should have placeholder. How do I set it up?
Try this: it should work:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Your_Title" message:#"Your_message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// Alert style customization
[[av textFieldAtIndex:1] setSecureTextEntry:NO];
[[av textFieldAtIndex:0] setPlaceholder:#"First_Placeholder"];
[[av textFieldAtIndex:1] setPlaceholder:#"Second_Placeholder"];
[av show];
You can access the values of the text fields on the alertView:clickedButtonAtIndex: of UIAlertViewDelegate.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"1 %#", [alertView textFieldAtIndex:0].text);
NSLog(#"2 %#", [alertView textFieldAtIndex:1].text);
}

multiple alert view in one view controller (tag doesn't work)

I have read some methods using alert view .tag, but that doesn't work.
The first alert:
-(void)addNewTask
{
UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:#"New Task"
message:nil
delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert1 show];
}
The Second Alert
-(void)changeTime:(int)value atRow:(int)p
{
TaskData *data = [tasks objectAtIndex:p];
NSLog([NSString stringWithFormat:#"%d %d",data.time,value]);
int time = data.time ;
time += value;
data.time = time;
NSLog([NSString stringWithFormat:#"%d %d",data.time,value]);
[self saveData:tasks];
[self.Taskview reloadData];
if(time>=5&&(data.leveljudge!=1))
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:nil
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
data.leveljudge=1;
[alert2 show];
}
Delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *name = [alertView buttonTitleAtIndex:buttonIndex];
if ([name isEqualToString:#"OK"])
{
UITextField *tf=[alertView textFieldAtIndex:0];
NSString *name = tf.text;
TaskData *newTask = [[TaskData alloc] init];
newTask.TaskName = name;
newTask.time = 0;
newTask.leveljudge=0;
[tasks addObject:newTask];
[self saveData:tasks];
[self.Taskview reloadData];
}
else if ([name isEqualToString:#"YES"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://d.blog.xuite.net/d/1/5/4/12103250/blog_1606564/txt/53796893/2.jpg"]];
}
}
The problem is the delegate only work for the first alert.
My app looks like: https://www.youtube.com/watch?v=WxlVKk0CTiQ
-(IBAction)flipAction:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is My Button" message:#" Hi" delegate:self cancelButtonTitle:#"OK " otherButtonTitles:#"Cancel", nil];
alert.tag=1;
[alert show];
[alert release];
}
-(IBAction)flipAction123:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is a flipAction123" message:#"this is message..." delegate:self cancelButtonTitle:#"OK " otherButtonTitles:#"Cancel", nil];
alert.tag=2;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
if (alertView.tag==1)
{
if (buttonIndex==0)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is another alert button" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
else
{
NSLog(#"paresh here..");
}
}
else if(alertView.tag==2)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is another alert alertView.tag ==2" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
}
For alert2, Delegate is not called because you have not set the delegate.
Try making the following changes.
Alert1
UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:#"New Task"
message:nil
delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
alert1.tag = 101; //Add a Tag to the alert
[alert1 show];
Alert2
Alert 2 delegate is not set properly
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:self //Set your delegate
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
alert2.tag = 102; //Add a different tag value to the second alert
data.leveljudge=1;
[alert2 show];
Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 101)
{
//Handle Alert 1
}
else if(alertView.tag ==102)
{
//Handle Alert 2
}
}
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:self //here should be self, not nil
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
data.leveljudge=1;
[alert2 show];

UIAlertView alertView.cancelButtonIndex = -1; Not Working

I am trying to create an AlertView which does not have a cancel button.
This is the code I have tried:
alertView.cancelButtonIndex = -1;
It does not seem to work with iOS 7.
Any ideas?
Thanks,
1) With using this code pressing "Do something button" can do some action but will not lead to alertView hiding
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles: #"Do something", nil] show];
#pragma mark - UIAlertViewDelegate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return NO;
}
2) With using this code there will be no buttons on the alertView(I think this is what you actually need)
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles:nil] show];

Retrieving tapped button on UIAlertView

I had this code running on my app, and now on iOS 6 it crashes my app:
// Another method
[NSThread detachNewThreadSelector:#selector(askServerForNFeID)
toTarget:self withObject:nil];
- (void)askServerForNFeID {
if ([response isEqualToString:#"XXXX"]) {
NSString *responseMessage = [NSString stringWithFormat:
NSLocalizedString(#"Autorizado o Uso da NFe\n para chave:\n%#", #""),
[invoiceInfo NFeID]];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Sefaz respondeu:", #"")
message:responseMessage
delegate:self
cancelButtonTitle:NSLocalizedString(#"OK", #"")
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
I have learned that calling the alert from a second thread causes the crash, and so I changed my code to call the alert from the main thread like this:
if ([response isEqualToString:#"XXXX"]) {
[self performSelectorOnMainThread:#selector(showAlertHelper:)
withObject:[[NSArray alloc] initWithObjects:
NSLocalizedString(#"Não posso seguir em frente", #""),
NSLocalizedString(#"Você usou .....", #""), #"Fechar", #"Comprar", nil]
waitUntilDone:YES];
Note I am parsing the title, message and buttons as a list to showAlertHelper...
-(void)showAlertHelper:(NSArray*)theArray{
if ([[theArray objectAtIndex:3] isEqualToString:#""]) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
[alertView show];
[alertView release];
}
}
My problem now, is that I had this handler to catch the button that was tapped, and it doesn't work any more. It just isn't called:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:NSLocalizedString(#"Comprar", #"")]) {
// Do stuff to buy credits...
} else if ([title isEqualToString:NSLocalizedString(#"Fechar", #"")]) {
NSLog(#"Fechar was selected.");
}
else if ([title isEqualToString:NSLocalizedString(#"Criar conta", #"")]) {
// Do stuff to create an account...
}
}
The problem is that in your re-written method, you set the delegate to nil instead of self. As a result, the alertView:clickedButtonAtIndex: delegate method isn't called. Change your code to the following: by specifying self in the delegate: parameter:
-(void)showAlertHelper:(NSArray*)theArray{
if ([[theArray objectAtIndex:3] isEqualToString:#""]) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:nil];
[alertView show];
[alertView release];
} else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
[alertView show];
[alertView release];
}
}
You removed the delegate from the creation method of your UIAlertView
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:nil
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];
should be
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:[theArray objectAtIndex:0]
message:[theArray objectAtIndex:1]
delegate:self
cancelButtonTitle:[theArray objectAtIndex:2]
otherButtonTitles:[theArray objectAtIndex:3], nil];

Using an NSString as UIAlertView message:

I can't figure out why this won't work, I've tried it 100 ways. The AlertView shows up with a blank message. Here's my code:
eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
timeTillRest is an NSString. and just before calling the alertview an NSLog(#"%#",timeTillRest); displays the string without trouble.
Why would you use an alertview as an instance variable? There's no need for that. It's just as easy as this:
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
[eventChoiceNow release];
This should work just fine. Testing using this code:
NSString *timeTillRest = #"Testing";
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
And it works fine. Also testing using:
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic,strong) NSString *timeTillRest;
#end
#implementation ViewController
#synthesize timeTillRest;
- (void)viewDidLoad
{
[super viewDidLoad];
timeTillRest = #"Testing";
UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:#"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[eventChoiceNow show];
// Do any additional setup after loading the view, typically from a nib.
}
And that works flawlessly too. Make sure you aren't setting that property to nil anywhere.