Xcode Buttons: Some work, some do not? - ios

I am attempting to design an app for my senior project, and am thus fairly new to iOS coding. I have created a .xib view controller, and added a number of buttons to it, over top of some card images. There is one large button over the other player's hand, and sever smaller ones over each of the player's own cards.
I have added UIAlertControllers to each button, intending the pop-ups to allow the player to play or discard their own cards, or send some information about the other player's.
The Alert pops up fine for the other player's button, but the ones for the bottom cards do absolutely nothing. I have checked that they are all connected to the IBActions, and the code is nearly identical to the button that IS working. Any ideas?
This is all contained within gameViewController.m: btnTop correctly performs its action, while btnBottom1 - btnBottom5 do nothing.
- (IBAction)btnTop:(id)sender{
NSLog(#"Top Button Pressed. Displaying AlertView.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Give Information" message:#"Would you like to give information about color or number?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionColor = [UIAlertAction actionWithTitle:#"Color" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self giveColorInfo];
}];
UIAlertAction *actionNumber = [UIAlertAction actionWithTitle:#"Number" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionColor];
[alertController addAction:actionNumber];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)btnBottom1:(id)sender{
NSLog(#"Bottom 1 Button Pressed. Displaying AlertController.");
UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:#"Play or Discard Card 1" message:#"Would you like to play or discard?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionPlay = [UIAlertAction actionWithTitle:#"Play"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self playCard:1];
}];
UIAlertAction *actionDiscard = [UIAlertAction actionWithTitle:#"Discard" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController2 addAction:actionPlay];
[alertController2 addAction:actionDiscard];
[alertController2 addAction:actionCancel];
[self presentViewController:alertController2 animated:YES completion:nil];
}
- (IBAction)btnBottom2:(id)sender {
NSLog(#"Bottom 2 Button Pressed. Displaying AlertView.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Play or Discard Card 2" message:#"Would you like to play or discard?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionPlay = [UIAlertAction actionWithTitle:#"Play"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self playCard:2];
}];
UIAlertAction *actionDiscard = [UIAlertAction actionWithTitle:#"Discard" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionPlay];
[alertController addAction:actionDiscard];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)btnBottom3:(id)sender {
NSLog(#"Bottom 3 Button Pressed. Displaying AlertView.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Play or Discard Card 3" message:#"Would you like to play or discard?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionPlay = [UIAlertAction actionWithTitle:#"Play"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self playCard:3];
}];
UIAlertAction *actionDiscard = [UIAlertAction actionWithTitle:#"Discard" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionPlay];
[alertController addAction:actionDiscard];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)btnBottom4:(id)sender {
NSLog(#"Bottom 4 Button Pressed. Displaying AlertView.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Play or Discard Card 4" message:#"Would you like to play or discard?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionPlay = [UIAlertAction actionWithTitle:#"Play"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self playCard:4];
}];
UIAlertAction *actionDiscard = [UIAlertAction actionWithTitle:#"Discard" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionPlay];
[alertController addAction:actionDiscard];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)btnBottom5:(id)sender {
NSLog(#"Bottom 5 Button Pressed. Displaying AlertView.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Play or Discard Card 5" message:#"Would you like to play or discard?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionPlay = [UIAlertAction actionWithTitle:#"Play"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self playCard:5];
}];
UIAlertAction *actionDiscard = [UIAlertAction actionWithTitle:#"Discard" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:actionPlay];
[alertController addAction:actionDiscard];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}

Related

How present UIMenuController and UIAlertController at the same time?

I'm trying next way:
Message *message = self.messagesArray[indexPath.row];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:LocalizedString(#"FirstAction")
message:#""
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *editMessage = [UIAlertAction actionWithTitle:LocalizedString(#"SecondAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:editMessage];
UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:LocalizedString(#"ThirdAction") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:forwardMessage];
UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:LocalizedString(#"DeleteMessage") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:deleteMessage];
[self setSourceViewForAlertController:alertController];
[self presentViewController:alertController animated:YES completion:nil];
[self createMenuForMessage:message];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
So, my aim is to present alertcontroller for UICollectionViewCell and UIMenuController at the same time.
Like this:
Your issue is related to where are you showing your UIMenuController you have to take in account that UIView must have implemented the canBecomeFirstResponder method returning YES
- (IBAction)action:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#""
message:#""
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *editMessage = [UIAlertAction actionWithTitle:#"Edit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:editMessage];
UIAlertAction *forwardMessage = [UIAlertAction actionWithTitle:#"Move" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:forwardMessage];
UIAlertAction *deleteMessage = [UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:deleteMessage];
[self presentViewController:alertController animated:YES completion:nil];
[[UIMenuController sharedMenuController] setTargetRect:self.view.bounds inView:self.view];
[[UIMenuController sharedMenuController] setArrowDirection:UIMenuControllerArrowDefault];
[[UIMenuController sharedMenuController] setMenuItems:#[[[UIMenuItem alloc]initWithTitle:#"test" action:#selector(didReceiveMemoryWarning)]]];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
-(BOOL)canBecomeFirstResponder{
return true;
}

How to Change the UIAlertAction button Color in iOS?

I've a UIAlertController and I've a bunch of UIAlertAcion Buttons. Now I need to show one button with other Color rather than the same color.
For Ex
Button1
Button2
Button3
Button1 and button3 should be in blue
and
button2 should be in red.
Is it possible ? How?
Just Throw your thoughts
...
My Code:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Food Menu" message:#"Select the MenuItem" preferredStyle:UIAlertControllerStyleActionSheet];
for(int i= 0; i<[menus count];i++){
UIAlertAction *action = [UIAlertAction actionWithTitle:[menu objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
//do SomeWork
}];
if(i==currentIndex){
//Put button Color Red
}
else{
//put button color Blue
}
[actionSheet addAction:action];
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheet addAction:cancel];
[self presentViewController:actionSheet animated:YES completion:nil];
}
do like change the alert style:UIAlertActionStyleDestructive
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Reset", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Reset action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:resetAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

Add custom view in UIAlertController in iOS

I have used below code to show action sheet with Tweet, Facebook and Cancel button.
- (void)shareApp:(id)sender {
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Now, I want to add custom view i.e. logo + tweet on each element of action sheet.
How can this be implemented?
I got the solution through your code.Just refer the below coding
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
UIImage *accessoryImage = [UIImage imageNamed:#"Twitter.jpg"];
[tweetAction setValue:accessoryImage forKey:#"image"];
UIImage *accessoryFBImage = [UIImage imageNamed:#"Facebook.png"];
[facebookAction setValue:accessoryFBImage forKey:#"image"];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES
completion:nil];

Text field on UIAlertController goes below the action buttons

So, I'm stumped...Here's the image for quick reference: http://i.imgur.com/zM1VJdi.png
The issue is that when a textField is added to a UIAlertController, the textfield's height is larger than one line, and goes underneath the UIAlertController buttons. Any thoughts on why this could be happening?
Here's the code:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *resetDataAction = [UIAlertAction actionWithTitle:#"Delete"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
// handler code
}];
[alertController addAction:resetDataAction];
[alertController addAction:cancelAction];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//handler code
}];
[self presentViewController:alertController animated:YES completion:nil];
because you add buttons then your text field.
[alertController addAction:resetDataAction];
[alertController addAction:cancelAction];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//handler code
}];
but you need to do it like that
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//handler code
}];
[alertController addAction:resetDataAction];
[alertController addAction:cancelAction];

Change Action sheet popover arrow in iOS8

i'm using UIAlertController . But on iPad with iOS 8, actionSheet show with popover arrow. Any ideas to hide that arrow?
Here is my code:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = self.view.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
}
[self presentViewController:alertController animated:YES completion:nil];
Solution :
use below line for remove arrow from action sheet
[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];
Sample
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Test Action Sheet" message:#"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
UIAlertAction *otherAction = [UIAlertAction
actionWithTitle:#"Other"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Otheraction");
}];
[alertController addAction:okAction];
[alertController addAction:otherAction];
[alertController addAction:cancelAction];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = self.view.bounds;
[self presentViewController:alertController animated:YES completion:nil];
Output
Jageen's answer, in Swift:
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
The selected answer does not center the alert if you have a nav/status bar. To exactly center your alert controller:
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = 0;
With the convenience method:
- (CGRect)sourceRectForCenteredAlertController
{
CGRect sourceRect = CGRectZero;
sourceRect.origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.origin.x/2.0;
sourceRect.origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.origin.y/2.0;
return sourceRect;
}
Also, the alert controller does not stay centered if the view is rotated. To keep the alert controller centered you need to update the sourceRect after rotation. For example:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check if your alert controller is still being presented
if (alertController.presentingViewController) {
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
}
}
Or, if you do not want to use rotation events, you can use the popoverPresentationController delegate method to reposition the popover:
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view
{
// Re-center with new rect
}
You're on the wrong track using UIPopoverPresentationController to display an alert. You just do not need that snipped of code...
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];

Resources