Change position UIAlertController (UIAlertControllerStyleActionSheet) - objective-c

i need to change the default postition where it shows the UIAlertControllerStyleActionSheet.
Default position is
I need to be
is that possible?

UIAlertController action row position will always depends on order of adding UIAlertAction.
Try below code :
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *FacebookAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Facebook", #"Facebook")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Show facebook action action");
}];
UIAlertAction *twitterAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Twitter", #"Twitter")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Show Twiter action");
}];
[alertController addAction:cancelAction]; // 1th row from bottom
[alertController addAction:twitterAction]; // 2nd row from bottom
[alertController addAction:FacebookAction]; // 3rd row from bottom

Related

Application tried to present modally an active controller, why this code is crashing, knows anything about it?

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:#"Select Language"
message:#""
preferredStyle:UIAlertControllerStyleActionSheet];
//We add buttons to the alert controller by creating UIAlertActions:
for (int j =0 ; j<val.count; j++)
{
NSString *titleString = val[j];
UIAlertAction * action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
You should only present the alert controller once. Move it out of your loop, and it should work.

Converting Swift block to Objective-C block

In Swift, I have created a closure method (I think):
func firstMove(action: UIAlertAction!) {
if action.title == "Yes" {
currentPlayer = "X"
} else {
currentPlayer = "0"
}
That I pass into this UIAlertAction method:
let optionToStartController = UIAlertController(title: "", message: "Do you want first move?", preferredStyle: .Alert)
optionToStartController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: firstMove))
How can I convert both the closure and method to Objective-C?
I have tried doing:
- (void)firstMove:(UIAlertAction*)action
{
if ([action.title isEqual: #"Yes"]) {
_currentPlayer = 'X';
} else {
_currentPlayer = 'O';
}
}
And passing it like this:
UIAlertController *optionToStartController = [UIAlertController alertControllerWithTitle:#"" message:#"Do you want first move?" preferredStyle:UIAlertControllerStyleAlert];
[optionToStartController addAction:[UIAlertAction actionWithTitle:#"Yes" style:UIAlertActionStyleDefault handler: firstMove]];
You may be looking for blocks, the objective-C rough equivalent of closures. I'm not quite sure what you're trying to accomplish, but the following code defines block firstMove and how it is passed and called from method addAction.
void(^firstMove)(int) = ^(int x){
NSLog(#"print y: %d", x);
};
[self addAction:firstMove];
...
-(void)addAction:(void(^)(int))y{
y(5);
}
You can see the example. It should work properly.
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Question"
message:#"Do you want first move?"
preferredStyle:UIAlertControllerStyleAlert];
//First button
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"YES"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Actions if the user press this button
//Dismiss the alertController. It's preferred to be in all actions.
[alert dismissViewControllerAnimated:YES completion:nil];
_currentPlayer = #"X";
}];
//Second Button
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"NO"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Actions if the user press this button
//Dismiss the alertController. It's preferred to be in all actions.
[alert dismissViewControllerAnimated:YES completion:nil];
_currentPlayer = #"O";
}];
//Add the actions to the alertController
[alert addAction:ok];
[alert addAction:cancel];
//present the alertController on the device. If you are writing this in the viewController, you can use self.view, if you are in UIView, then just self
[self.view presentViewController:alert animated:YES completion:nil];

how to pass parameter on completion block in objective-c

I am trying to declare a function with couple of blocks inside it, when I will call the function from viewdidload method, these parameters should perform accordongly
-(void) alertViewOn:(UILabel*)LabelTextX :(UIButton*)ButtonA :(UIButton*)ButtonB :(UIImage*)ImageX {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Enter Pet Name" message:nil preferredStyle:UIAlertControllerStyleAlert];
//Show TextField
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder = NSLocalizedString(#"Pet Name", #"Name");
}];
//Set Ok Button
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
UITextField *textField = alert.textFields.firstObject;
self.labelText2.text = textField.text;
}];
[alert addAction:ok];
//Set ADD Button
UIAlertAction* addPet = [UIAlertAction actionWithTitle:#"Add More" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
self.button4.hidden = NO;
self.button5.hidden = NO;
self.image2.hidden = NO;
}];
[alert addAction:addPet];
//Set Cancel Button
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
Here I want to pass the parameter LabelTextX to labelText2 ; ButtonA to button4 ; ButtonY to button5 and ImageX to image2 . I am totally new in objective-c and finding it difficult even to declare a function !!! Please any kind person help ....
-(void) alertViewOn:(UILabel*)LabelTextX :(UIButton*)ButtonA : (UIButton*)ButtonB :(UIImageView*)ImageX {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Enter Pet Name" message:nil preferredStyle:UIAlertControllerStyleAlert];
//Show TextField
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder = NSLocalizedString(#"Pet Name", #"Name");
}];
//Set Ok Button
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
UITextField *textField = alert.textFields.firstObject;
LabelTextX.text = textField.text;
}];
[alert addAction:ok];
//Set ADD Button
UIAlertAction* addPet = [UIAlertAction actionWithTitle:#"Add More" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
ButtonA.hidden = NO;
ButtonB.hidden = NO;
ImageX.hidden = NO;
}];
[alert addAction:addPet];
//Set Cancel Button
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
And then called the function like this ...
- (IBAction)buttonPressed15:(id)sender {
[self alertViewOn:_labelText15 :_button30 :_button31 :_image15];
}

Thread1:EXC_BAd_InSTRUCTION(code =EXC_1386_INVOP,subcode)

I'm new to Cocoa, xcode.I'm Doing Sample Project " How to display AlertPanel and Alert Sheets.I am Getting Error Like this "thread1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=...).Here i Mentioned the Code line where i got the error.Please help me out.
Alert.beginSheetModalForWindow(window,completionhandler:{(code:NSMOdalResponse)-> void in.
NSAlert beginSheetModalForWindow is for Mac OS development.
As you mentioned iPhone as a tag to this question, I assume you are developing iOS application. For iOS development, use UIAlertController. Here is the sample code:
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handel yes button action here
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"No"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handel no button action here
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
For more details, refer Apple iOS Documentation
Hope this helps.

How to use Action Sheet in objective c using xcode 7?

UIActionSheet is showing it's deprecated in Xcode 7, how can I address this problem?
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped do nothing.
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Take photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// take photo button tapped.
[self takePhoto];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Choose photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// choose photo button tapped.
[self choosePhoto];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Delete Photo" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self deletePhoto];
}]];
You can use this code in place of UIActionsheet :)
You have to use UIAlertController with preferredStyle is UIAlertControllerStyleActionSheet because UIActionSheet is deprecated in iOS8 and this is iOS version issue not Xcode version issue.
You have to use UIAlertController with preferredStyle UIAlertControllerStyleActionSheet because UIActionSheet is deprecated in iOS8 and this is an iOS version issue not Xcode version issue.