How to present a SLCompose view controller from NSObject Class - objective-c

Here is my code :
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *viewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social platform to use it, e.g. facebook or twitter
[viewController setInitialText:sQuotes];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navController presentViewController:viewController animated:YES completion:nil];
[viewController setCompletionHandler:^(SLComposeViewControllerResult result)
{
NSString *output;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
{
output = #"Post Successfull";
}
break;
default:
break;
}
[appDelegate.navController dismissViewControllerAnimated:YES completion:nil];
}];
}
But it Shows the warning
on whose view is not in the window hierarchy!

The warning is pretty clear. You are trying to present a modal view by a view that is not in the window hierarchy, which won't work.
Try changing this [appDelegate.navController presentViewController:viewController animated:YES completion:nil];
to something like this instead:
[appDelegate.window.rootViewController presentViewController:viewController animated:YES completion:nil];
This guarantees that the currently active root view controller of your app will be the one presenting the modal view controller.
Depending on how your AppDelegate is built you may need to add a property or just a getter to surface the window variable to the outside world.

Related

xcode 9: presentViewController removing all previous controller

I have view controller and implement like this :
loginViewController = [[LoginViewController alloc] init];
loginViewController.delegate = self;
[self.view addSubview:loginViewController.view];
[self addChildViewController:loginViewController];
[loginViewController didMoveToParentViewController:self];
Inside loginViewController, I want to show another view controller but I implement like this :
otherViewController = [[OtherViewController alloc] initWithNibName:nil bundle:nil];
otherViewController.delegate = self;
[self presentViewController:otherViewController animated:YES completion:nil];
Everytime I done with otherViewController and want to dismiss the view controller [self dismissViewControllerAnimated:YES completion:nil];
it restarted from the beginning and not showing the loginViewController. I tried to check using Debug View Hierarchy and got that no loginViewController is implemented when I called presentViewController:otherViewController
Why is this happened ?

Custom NSMenuItem-views glitch when scrolling NSMenu on low resolution screen

I'm having some glitchy bugs when I'm using my menu bar application on a low resolution screen. Please see screenshots below as illustration! Because of the reduced height of the lo-res screen my total NSMenu does not fit in the screen, and so automatically a scrollview appears so i can still get to all the different items.
The problem is that my cutsom NSMenuItems, which thus have custom NSViews with their own draw [drawRect:(NSRect)dirtyRect] method, are behaving glitchy when scrolling down, and back up. It's as if there are some drawing calls missing. I unfortunately do not know how to detect when the NSMenu is effectively scrolling, or simply to access this automagically created scrollview on the NSMenu class...
Any help much appreciated !
the drawing code of my view in my NSMenuItem.
- (void)drawRect:(NSRect)dirtyRect
{
if (self.enabled)
{
if (self.isHighlighted)
{
[self colorRectHighlighted:dirtyRect];
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateHiglighted];
[self drawStateIconWithColor:[NSColor whiteColor]];
}
else
{
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateNormal];
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
if(appDelegate.menuIsInDarkMode)
{
[self drawStateIconWithColor:[NSColor colorWithWhite:1.0 alpha:0.9]];
}
else
{
[self drawStateIconWithColor:[NSColor selectedMenuItemIndicatorColor:([NSColor currentControlTint]==NSBlueControlTint)?YES:NO]];
}
}
}
else
{
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateNormal];
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
if(appDelegate.menuIsInDarkMode)
{
[self drawStateIconWithColor:[NSColor colorWithRed:136/256.0 green:136/256.0 blue:136/256.0 alpha:0.9]];
}
else
{
[self drawStateIconWithColor:[NSColor colorWithWhite:0 alpha:0.1]];
}
}
}
EDIT:
I initialise the NSMenuItems with the sliders as this:
self.tiltSliderViewController = [[SlidingViewController alloc] initWithNibName:#"SlidingViewController" bundle:nil];
self.tiltSliderViewController.delegate = self;
[tiltSliderMenuItem setView:[self.tiltSliderViewController view]];
self.rotationSliderViewController = [[SlidingViewController alloc] initWithNibName:#"SlidingViewController" bundle:nil];
self.rotationSliderViewController.delegate = self;
[rotationSliderMenuItem setView:[self.rotationSliderViewController view]];
The viewcontrollers used here have the following implementation:
#interface SlidingViewController ()
#end
#implementation SlidingViewController
- (IBAction)valueChanged:(id)sender
{
float slider_value = [_slider floatValue];
[_slider setToolTip:[NSString stringWithFormat:#"%ld",
_slider.integerValue]];
[_delegate sliderValueChanged:slider_value sender:self];
}
#end
screenshots:
NORMAL
DOWN
UP AGAIN = GLITCH!!

Objective-C: How to make a button take you to another class/storyboard

I want to make the bottom button seen in the storyboard on the right to take the user to the signupstoryboard.storyboard/signupController seen on the left when the user taps on the button. How can that be done?
{ - (IBAction)signupbutton:(id)sender; SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil]; }
You'll have to do it by code.
- (IBAction)buttonTapped
{
NSString *storyboardName = #"signupstoryboard";
UIViewController *nextViewController = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateInitialViewController];
// if you are using a navigation controller to push it:
[self.navigationController pushViewController:nextViewController animated:YES];
// otherwise if you are presenting it modally:
[self presentViewController:nextViewController animated:YES completion:nil]
}
In your .m (at the top)
#import "signupController.h"
In your button action method (Don't forget to set the storyboard id for your signupController as signup
- (IBAction)signupbutton:(id)sender
{
SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil];
}
Thats it

dismissViewControllerAnimated does not work in iOS 7?

I have a cell presenting a Passcode change view controller when tapped.
else if (indexPath.row == 2) {
//Change passcode
NSString *passcode = [[NSUserDefaults standardUserDefaults] stringForKey:#"passcode"];
PAPasscodeViewController *passcodeViewController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionChange];
passcodeViewController.delegate = self;
passcodeViewController.passcode = passcode;
passcodeViewController.simple = YES;
[self presentViewController:passcodeViewController animated:YES completion:nil];
break;
}
a delegate callback method to dismiss the Passcode change view controller when hit cancel:
- (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
// [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
// [self.navigationController popViewControllerAnimated:YES];
}
it does not however dismiss the Passcode change view controller when hit the cancel button even though xcode debug hits the code. I tried [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; it didn't work either. The presentingViewController property is nil.
It worked perfectly on iOS 6 before. Anyone ran into this problem before? Thanks
Your can try this in PAPasscodeViewController, instead of using the delegate,.
[self removeFromParentViewController];

Open ViewController from different class

I want to be able to open a ViewController from a different class. So I could simply call it to open a view wherever I need it.
So I have this setup in the class that holds the code:
+ (void)openCalcView: (NSString *)nameOfView {
UIViewController *controller;
if ([nameOfView isEqualToString:#"Tax"]) {
controller = [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
}else if ([nameOfView isEqualToString:#"Rent"]){
controller = [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
}
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
But [self presentViewController:controller animated:YES completion:nil]; gives me a warning:
Class method '+presentViewController:animated:completion:' not found (return type defaults to 'id')
I can call simple things like NSLog through this, from any class. But this doesn't work.
Because openCalcView is a Class method, there is no UIViewController instance (i.e. no [self presentViewController:] method).
You'll need to also pass a UIViewController to this class method, something like this:
+ (void)openCalcView: (NSString *)nameOfView fromViewController:(UIViewController *)controller {
UIViewController *newController;
if ([nameOfView isEqualToString:#"Tax"]) {
newController= [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
}else if ([nameOfView isEqualToString:#"Rent"]){
newController= [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
}
newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[controller presentViewController:newController animated:YES completion:nil];
[newController release];
}
the controller parameter represents the UIViewController that is opening the new view controller