Actionsheet buttons do not respond when using showInView - objective-c

I have this code on my program
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *saveMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[saveMessageBtn setImage:[UIImage imageNamed:#"btn_done.png"] forState:UIControlStateNormal];
[saveMessageBtn addTarget:self action:#selector(saveMessage) forControlEvents:UIControlEventTouchUpInside];
[saveMessageBtn setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveMessageBtn];
}
-(IBAction)saveMessage:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSLog(#"Send Now");
}
else if (buttonIndex == 1){
[self performSegueWithIdentifier:#"modalNonRecurring" sender:self];
}
else if (buttonIndex == 2){
[self performSegueWithIdentifier:#"modalRecurring" sender:self];
}
else{
NSLog(#"Cancel Clicked");
}
}
as you can see in the code, it supposed to perform a segue or do 'NSLog' when a particular button clicked.
But when I click a button, it does not perform what I want it to do, instead it displays this message in the debug area..
Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].
By the way, I am using a UINavigationController that is inside a UITabBarController.
Anyone that has a great idea how to fix this? your help would be much appreciated. Thanks!

Add UIActionSheetDelegate delegate in .h file and try like this then it'l work.
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
actionSheet.delegate = self;
[actionSheet showInView:self.view];

Related

Extracting input from a UIAlertView text box Objective-C

I have gotten too frustrated from trying to find an answer to this, so I will as a question...
How can I get the raw text from a UIAlertView text box in Objective-C?
Here is my code:
UIButton *AppCrashButton = [UIButton buttonWithType: UIButtonTypeCustom];
AppCrashButton.frame = CGRectMake(0, (self.view.frame.size.height - 44) / 6 * 1, self.view.frame.size.width, (self.view.frame.size.height - 44) / 6);
[AppCrashButton setTitle: #"Ping" forState: UIControlStateNormal];
[AppCrashButton addTarget: self action: #selector(Click) forControlEvents: UIControlEventTouchUpInside];
AppCrashButton.backgroundColor = [UIColor colorWithRed:0.19 green:0.19 blue:0.19 alpha:1.0];
AppCrashButton.layer.borderColor = [UIColor colorWithRed:0.96 green:0.26 blue:0.21 alpha:1.0].CGColor;
AppCrashButton.layer.borderWidth = 0.5f;
[self.view addSubview: AppCrashButton];
-(void) Click {
UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: #"IP Ping" message: #"Please enter the IP address" delegate: self cancelButtonTitle: #"Cancel" otherButtonTitles: #"OK", nil];
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[AppCrashAlert show];
[AppCrashAlert release];
}
It shows a custom button, that once clicked, executes the "Click" method, which pings a host.
I was wondering how I could get the text from the text input.
Thanks
Set UIAlertViewDelegate delegate to Your controller
#interface YourViewController ()<UIAlertViewDelegate>
After that when you create your UIAlertView set it's delegate to Self In your case here
UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: #"IP Ping" message: #"Please enter the IP address" delegate: self cancelButtonTitle: #"Cancel" otherButtonTitles: #"OK", nil];
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
// Set delegate
AppCrashAlert.delegate = Self;
[AppCrashAlert show];
[AppCrashAlert release];
Then,
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex(NSInteger)buttonIndex{
NSLog(#"Text : %#",[[alertView textFieldAtIndex:0] text]);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
To get entered text in UIAlertView text field

How to show the actionSheet originating from UiButton

I have a uiButton and i want to show a action sheet originating from it.
This is my code for close button,
+ (void)addLeftButton:(UINavigationItem *)navItem withTarget:(id) navTarget
{
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:-5];
UIImage *buttonImage = [UIImage imageNamed:#"close-button.png"];
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
closeButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
[closeButton addTarget:navTarget action:#selector(didSelectCloseButton:) forControlEvents:UIControlEventTouchUpInside];
[navItem setLeftBarButtonItems:[NSArray arrayWithObjects:fixedSpace,aBarButtonItem,nil]];
}
This IBAction for the close button,
(IBAction)didSelectCloseButton:(id)sender
{
if([self->_customer getCheckInDraft])
{
[self showActionSheet:sender];
}
and in showActionSheet I have this code,
(void) showActionSheet:(id)sender
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[self->_actionSheet showFromBarButtonItem:sender animated:YES];
}
//Here the showFromBarButtonItem gets the sender as uiButton but it crashes.on clicking the Close button the app crashes.
Here is action sheet code.
- (IBAction)showActionSheet:(id)sender {
/* Dismiss keyboard*/
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"What ever you want to ask?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete it" otherButtonTitles:#"Copy", #"Duplicate", nil]; //Change the options that you want to display as per your requirement.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// In this case the device is an iPad.
[actionSheet showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
}
else{
// In this case the device is an iPhone/iPod Touch.
[actionSheet showInView:self.view];
}
}
#pragma mark - UIActionSheet method implementation
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"Index = %d - Title = %#", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"Action sheet did dismiss");
}
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"Action sheet will dismiss");
}
/* UPDATE */
Above is deprecated in iOS 8.3 and above so you could use below UIAlertController.
Also if your keyboard is open while user tap on button then the keyboard dismiss automatically and let the UIAlertController show options.
UIAlertController *alertController;
UIAlertAction *destroyAction;
UIAlertAction *otherAction;
alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
destroyAction = [UIAlertAction actionWithTitle:#"Remove All Data"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
// do destructive stuff here
}];
otherAction = [UIAlertAction actionWithTitle:#"Blah"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// do something here
}];
// note: you can control the order buttons are shown, unlike UIActionSheet
[alertController addAction:destroyAction];
[alertController addAction:otherAction];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
UIButton *btn = (UIButton *)sender;
popPresenter.sourceView = btn;
popPresenter.sourceRect = [btn bounds];
[self presentViewController:alertController animated:YES completion:nil];
You can do it like that:
UIAlertAction *action1;
UIAlertAction *actionCancel;
action1 = [UIAlertAction actionWithTitle:#"action1" style:UIAlertActionStyleDefault handler:nil];
actionCancel = [UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDestructive handler:nil];
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:#"alert" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
alertView.modalPresentationStyle = UIModalPresentationPopover; // for iPad
[alertView addAction:action1];
[alertView addAction:actionCancel];
if (viewSourse) { // Here your button
alertView.popoverPresentationController.sourceView = viewSourse;
alertView.popoverPresentationController.sourceRect = CGRectMake(viewSourse.frame.size.width/2, viewSourse.frame.size.height/2, 0, 0);
}
[self presentViewController:alertView animated:YES completion:nil];

My Tweak Isn't Executing

EDIT:
Running on an iPad Mini on iOS 8.1
I tried a test project to hook viewDidLoad in the app to show a UIAlertView, but nothing happened. Does anyone have idea what could cause the tweak to compile but not run properly?
10 hours of debugging, I'm turning to SO.
There's obviously something wrong with this code, and I have no idea what could be causing it. I'm hooking the TestOut view in the ChineseSkill app, but when I get to the view it acts as if nothing changed from the original app.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface TestOutViewController : UIViewController <UIAlertViewDelegate>
- (UIButton *)quitButton;
#end
%hook TestOutViewController
- (void)viewDidAppear:(BOOL)view {
%orig;
UIButton *infiniteLivesButton = [UIButton buttonWithType:UIButtonTypeSystem];
[infiniteLivesButton setTitle:#"Infinite Lives" forState:UIControlStateNormal];
[infiniteLivesButton setFrame:CGRectMake(/*[self quitButton].frame.origin.x + [self quitButton].frame.size.width, [self quitButton].frame.origin.y + 20*/50, 50, 200, 50)];
[infiniteLivesButton setBackgroundColor:[UIColor redColor]];
[infiniteLivesButton addTarget:self
action:#selector(makeLivesInfinite:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *passTestButton = [UIButton buttonWithType:UIButtonTypeSystem];
[passTestButton setTitle:#"Skip Test" forState:UIControlStateNormal];
[passTestButton setFrame:CGRectMake(infiniteLivesButton.frame.origin.x + infiniteLivesButton.frame.size.width + 10,
infiniteLivesButton.frame.origin.y,
150,
50)];
[passTestButton addTarget:self
action:#selector(skipTest:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:passTestButton];
[self.view addSubview:infiniteLivesButton];
UIAlertView *successAlertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Infinite hearts for the rest of this test." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[successAlertView show];
}
- (void)touchesBegan:(id)began withEvent:(id)event {
UIAlertView *testView = [[UIAlertView alloc] initWithTitle:#"Test" message:#"Test Alert" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[testView show];
}
%new
- (void)makeLivesInfinite:(UIButton *)sender {
[self performSelector:#selector(setNNumberofLeftHearts:) withObject:#1000000];
UIAlertView *successAlertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Infinite hearts for the rest of this test." delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[successAlertView show];
}
%new
- (UIButton *)quitButton {
return objc_getAssociatedObject(self, #selector(quitButton));
}
%new
- (void)skipTest:(UIButton *)sender {
[self performSelector:#selector(setNTotalTest:) withObject:#1];
[self performSelector:#selector(CheckOrContinueButtonClick:) withObject:NULL];
[self performSelector:#selector(CheckOrContinueButtonClick:) withObject:NULL];
}
%end
There's no alert views, no added views, no nothing. Nearly anything will help.
Your problem is you have to set the right filters in your tweak's plist file. By default the plist file has a bundle identifier filter com.apple.springboard. You should change this to the bundle identifier of the app you want your code injected.
Furthur information (Just a little):
What is happening in behind? MobileLoader is responsible for patching your code into a program. These plist files tell MobileLoader to patch the code in which program(s), in which framework(s) or in which running process. So your process is the app and not SpringBoard. This link
has good explanation about CydiaSubstrate (MobileSubstrate). You could also have a look at its documentation.

Still Dismissing Alert

I am trying to keep this UIAlertView up after the presses either of the buttons (1 & 2).
Once i click on the "+" button or the "-" button, I can see the UILabel text increment, then it closes the UIAlertView.
This is what i currently am using:
#pragma Alert View Methods
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
[self dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
#pragma count functions
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1 || buttonIndex == 2) {
return;
}
else{
[self dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self.currentCountButtonCount++;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",self.countButtonCount + 1]];
}if (buttonIndex == 2) {
self.currentCountButtonCount--;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",self.countButtonCount - 1]];
}
}
- (IBAction)countClick:(id)sender {
// tallies and keeps current count number
if (!self.currentCountButtonCount)
self.currentCountButtonCount = 0;
NSString *alertMessage = [NSString stringWithFormat:#"%d", self.countButtonCount];
self.countAlert = [[UIAlertView alloc]initWithTitle:#"Count" message:alertMessage delegate:self cancelButtonTitle:#"end" otherButtonTitles:#"+",#"-", nil];
[self.countAlert show];
}
On my last question someone told me to do it custom and this is what im trying now and it still dismisses the UIAlert.
How can i keep it up while the label changes until they touch the end button?
What are you using is default button of AlertView and after click on that button it will automatically dismissed the alertview.
So you have to create your buttons programatically and add that buttons in your alertview like:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn setTitle:#"+" forState:UIControlStateNormal];
[countAlert addSubview:btn ];
on this btn call your method.
So you have to create two custom button with "+" and "-". and add that buttons in AlertView.
-(void)setAlertValue:(id)sender{
switch ([sender tag]) {
case 1:
{
// currentCountButtonCount++;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",++countButtonCount]];
}
break;
case 2:
{
//currentCountButtonCount--;
[self.countAlert setMessage:[NSString stringWithFormat:#"%d",--countButtonCount]];
}
break;
default:
break;
}
}
- (IBAction)countClick:(id)sender {
// tallies and keeps current count number
if (!currentCountButtonCount)
currentCountButtonCount = 0;
NSString *alertMessage = [NSString stringWithFormat:#"%d", countButtonCount];
self.countAlert = [[UIAlertView alloc]initWithTitle:#"Count" message:alertMessage delegate:self cancelButtonTitle:#"end" otherButtonTitles:nil, nil];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 40, 20)];
[btn addTarget:self action:#selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor greenColor]];
btn.tag = 1;
[btn setTitle:#"+" forState:UIControlStateNormal];
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(230, 50, 40, 20)];
[btn1 addTarget:self action:#selector(setAlertValue:) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundColor:[UIColor redColor]];
btn1.tag = 2;
[btn1 setTitle:#"-" forState:UIControlStateNormal];
[countAlert addSubview:btn];
[countAlert addSubview:btn1];
[self.countAlert show];
}
I don't want to rain in your parade, but if you think that an alert view is the best way to handle an increment/decrement of a variable, I would suggest you to reconsider your design.
UIAlertViews are meant for transient information and simplified decision making. A simple "Are you sure?" is the text-book example of an alert view usage.
From the user stand point, it's much more comforting being able to modify all the attributes in sliders or any other form of permanent input, and then, when sure, hit the confirm button with an alert view (or confirmation screen). Doing it in an Alert view is not only error prone, but counter-intuitive compared in the way that the rest of iOS works.
If you're having trouble on fitting another form of input in your application, please read on how to perform animations and reveal control as they are needed, hiding the input inside an UIAlertView is simply the easiest solution for you, but not the best for the user.

how can an UIAlertView button calls another function?

-(void)buPressed{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Game Over"
message:#"YOU LOST! ALL YOUR BASE ARE BELONG TO US!"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Publish", nil];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex==0){
NSLog(#"%d",buttonIndex);
}
else{
[self bPressed];
}
}
-(void)bPressed{
ModalViewConroller *yeniSayfa=[[ModalViewConroller alloc] init];
yeniSayfa.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:yeniSayfa animated:YES];
[yeniSayfa release];
//Restore to Defaults
[button_1 setSelected:NO];
[button_2 setSelected:NO];
[button_3 setSelected:NO];
[button_4 setSelected:NO];
[button_5 setSelected:NO];
[button_6 setSelected:NO];
slider.value=50.00;
UIImage *image = [UIImage imageNamed:#"Smiley_00025.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(81, 43, image.size.width, image.size.height);
[self.view addSubview:imageView];
}
This is my code i want to make the publish button to call bPressed function but it is giving a warning and the program crashes when i touch the publish button i want to open a modalview when i push the publish button can anybody help me?
You need to declare the function in your header file so that other objects (in this case an instance of UIAlertView, since its delegate is set to your class) know that this method exists.
So, in your whatever_class.h file, add the following line below the #interface{ }:
-(void)bPressed;