Why error is showing on click of button? - objective-c

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates when I am using UIActionsheet on click of button in ios? Why?
- (IBAction)btnAboutUsActions:(UIButton *)sender
{
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate:self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Achievment",
#"Brown Story", nil];
actionSheet.tag = 100;
[actionSheet showFromRect: sender.frame inView: sender.superview animated: YES];
}

Use alertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:confirmText message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *achievment = [UIAlertAction actionWithTitle:#"Achievment"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction: achievment];
[self presentViewController:alert animated:YES completion:nil];
[alert.view layoutIfNeeded]

Related

iOS 11 UIAlertview not show full Message

I try to make a Popup for my app with UIAlertView. This Popup display fine on iOS 10 or older but on iOS 11 it doesn't display all the content of the popup. What can I do to fix that error!?
And this is the code that I am using to make the custom UAlertView. Any ideas?
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:nil delegate:self cancelButtonTitle:NSLocalizedString(#"ok", nil) otherButtonTitles: nil];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0, 900, 900)];
[lbl setAttributedText:getPopUp];
[lbl setNumberOfLines:0];
[lbl sizeToFit];
[lbl setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bg-popup-2.png"]]];
[alert setValue:lbl forKey:#"accessoryView"];
[alert show];
Sorry for my poor English!
Regards! thanks for all your help!!
Use UIAlertController instead of UIAlertView.
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:title //alert Title
message:getPopUp //Your Message
preferredStyle:UIAlertControllerStyleAlert];
UIView *firstSubview = alert.view.subviews.firstObject;
UIView *alertContentView = firstSubview.subviews.firstObject;
for (UIView *subSubView in alertContentView.subviews) {
subSubView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg-popup-2.png"]];
}
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
Hope this will help you.
For more details: http://hayageek.com/uialertcontroller-example-ios/
UIAlertView has been deprecated. Instead of using UIAlertView you should use UIAlertController like this :-
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:#"Title" message:#"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk=[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Ok Button Code
}];
UIAlertAction *actionCancel=[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//Cancel Button Code
}];
[alertController addAction:actionOk];
[alertController addAction:actionCancel];
[self presentViewController:alertController animated:YES completion:nil];

How to give toast message in objective c

I am trying to give a toast message when users login and user will able to see a toast message saying New Data Available and the button beneath that says Download and Cancel.But when Download comes its font Coming in BOLD.PLease let me know how can i make this look in normal by default fonts
Code as follows:
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:msg message:#""delegate:self cancelButtonTitle:#"Download" otherButtonTitles: #"Ignore",nil];
I need the Download should Come in normal fonts in toast message.
I have a Simple code to giving Toast.
- (void) ShowAlert:(NSString *)Message {
UIAlertController * alert=[UIAlertController alertControllerWithTitle:nil
message:#""
preferredStyle:UIAlertControllerStyleAlert];
UIView *firstSubview = alert.view.subviews.firstObject;
UIView *alertContentView = firstSubview.subviews.firstObject;
for (UIView *subSubView in alertContentView.subviews) {
subSubView.backgroundColor = [UIColor colorWithRed:141/255.0f green:0/255.0f blue:254/255.0f alpha:1.0f];
}
NSMutableAttributedString *AS = [[NSMutableAttributedString alloc] initWithString:Message];
[AS addAttribute: NSForegroundColorAttributeName value: [UIColor whiteColor] range: NSMakeRange(0,AS.length)];
[alert setValue:AS forKey:#"attributedTitle"];
[self presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:^{
}];
});
}
Use
[self ShowAlert:#"Please Enter Your Good Name."];
check it
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:#"your text"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[self performSelector:#selector(abc:) withObject:alert afterDelay:2];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"Download"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil]
;
delegate method for automatic disappear
-(void)abc:(UIAlertController*)x{
[x dismissViewControllerAnimated:YES completion:nil];
}
As per your requirement you need to create one custom Alert View which match your expectations.
For you reference you can choose custom alerts which allow features which you want. (see example links below)
1) Example_1: https://github.com/cs-joao-souza/JPAlertController
2) Example_2: https://github.com/kubacizek/ScrollableDisclaimerAlert
3) Example_3: https://github.com/gokulgovind/GLInAppPurchase
Hope it helps.

How to display UIActionSheet on the Bottom of ipad same as iphone in objective c

I am new to iOS and I am facing a problem regarding UIActionSheet.
My code is like this
-(IBAction)picimgbtnClick:(id)sender
{
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"Profile Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:
#"Gallery",
#"Camera",
#"Remove Photo",
nil];
popup.tag = 1;
[popup showInView:self.view];
}
This works perfectly on iPhone but when I run this on an iPad, the UIActionSheet appears in the middle (see image below) and also doesn't show a cancel button:
On iPhone, it appears like this:
At first UIActionSheet is now deprecated so use latest code for it.
- (IBAction)capture:(UIButton *)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// Code for Cam
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Gallery" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Code for Gallery
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Remove Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Code for removing photo
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds; // You can set position of popover
[self presentViewController:alert animated:TRUE completion:nil];
}
And as #balkaran singh said it's a default behaviour for ipad . you can not change it. If you want then you concrete your custom class and by animation you can get exact UI what you need.
Here methods showInView and showFromRect makes differences where on iPhone you will get the cancel option using showInView method but the same function you use in iPad you will not get.
Here are some methods you can customize your actionSheet origin see below image:
UIActionSheet's showFromRect:inView:animated: method is only supposed to be used on iPad. Its behavior on iPhone is undefined.
Hope this helps you.
You cannot show an action sheet from the bottom on the iPad, what you can do is show an alert.
The UIActionSheet class in deprecated since iOS 8, and should no longer be used. Instead you should make use of the UIAlertController. This works almost the same way:
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:#"Profile Photo"
message:#"Select method of input"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:#"Gallery"
style:UIAlertActionStyleDefault
handler:^void (UIAlertAction *action) {
NSLog(#"Clicked Gallery");
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Camera"
style:UIAlertActionStyleDefault
handler:^void (UIAlertAction *action) {
NSLog(#"Clicked Camera");
}]];
[alert addAction:[UIAlertAction actionWithTitle:#"Remove Photo"
style:UIAlertActionStyleDefault
handler:^void (UIAlertAction *action) {
NSLog(#"Clicked Remove Photo");
}]];
[self presentViewController:alert animated:YES completion:nil];
This will present the options as an alert, which is analogous to the alert sheet on the iPhone:
There is no supported way of showing it the exact same way (from the bottom) as on the iPhone. If you think about it, it is very hard to reach the bottom of the iPad from many ways of holding it. The center makes much more sense.

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];

Dismiss actionSheet before action under it's button is completed

My problem is that i have an ActionSheet, which is disappearing from screen only when action under this button is completed. My problem is that i want to click 'save' inside my action sheet, then dismiss action sheet and then to show some alert, informing user to wait until saving is done. Currently it works different: firstly action sheet is shown, then there is saving message UNDER action sheet, finally action sheet is removed from view.. so user doesnt see any alert message.
How to dismiss actionSheet earlier than xcode does it?
Method under sheetActionButton:
- (IBAction)saveAction:(id)sender
{
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
[self saveImageToCameraRoll];
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
You should move your saveImageToCameraRoll method onto a separate thread, or at least asynchronously on the main thread. Then you can dismiss the alert and saveAction: can return before it completes.
The simplest way to do this would be using dispatch_async. Use dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) to get a queue for a separate thread, or dispatch_get_main_queue() for the main thread. Make sure not to do any UI work (or use any APIs which aren't thread-safe) on other threads!
Edit: more detail:
- (IBAction)saveAction:(id)sender {
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:#"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save in the background
[self saveImageToCameraRoll];
dispatch_async(dispatch_get_main_queue(), ^{
// Perform UI functions on the main thread!
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
}