how to validate from UIButton addtarget to a different view controller - ios

I'm adding a UIButton to a view on a different UIViewController. I need the target selector to point to that view controller from json value
- (IBAction)timeline:(id)sender {
if ([_NoTimeline isEqualToString:#"timeline not available :)"]) {
NearmeNOViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"NONearmeViewController"];
[self presentViewController:vc animated:YES completion:nil];
} else {
NearmeFrendViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"NearmeFrendViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
}

Here you are assigning NSString "timeline not available :)" to variable self.NoTimeline if timeline isnt available. And assigning an NSArray to variable self.NoTimeline. If you can edit your api add status key properly. If not try this
- (IBAction)timeline:(id)sender{
if ([self.NoTimeline isKindOfClass:[NSString class]]) {
NearmeNOViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"NONearmeViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
else {
NearmeFrendViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"NearmeFrendViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
}

The problem here seems to be that _NoTimeline is not always of type NSString. Therefore the isEqualToString: operation fails.
You can check for NSString by using:
if ([_NoTimeline isKindOfClass:[NSString class]] {
// Your code
}

Related

Fix Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy

Currently, I have set up a log-in view for users of my app. Below is the code that presents this log-in view to the user:
// Handle how we present the view
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[NotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
WebViewController *wvc = [[WebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
} else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
What I'm attempting to do now is display a web-view first if the user has just updated the app. Below is what this new code looks like:
// Handle how we present the view.
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[SWNotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *wvc = [[SWWebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
else if (![versionOfLastRun isEqual:currentVersion])
{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSString *url=#"http://someurl";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if (versionOfLastRun == nil){
// First start after installing the app
}
else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
However, when I run the app, the web view is not displayed, I get a warning of:
Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy!
Can anyone help me diagnose the problem? Thank you!
Seems like your viewController is not in Navigation controller hierarchy. This means that your controller is not connected with navigation controller stack which should handle presented controllers.
The other thing that should help is setting your root view controller as follows:
self.window.rootViewController = self.ViewController;
EDIT: since you mention that you don't use storyboards, setting rootViewController should do the trick.
Try to set the window's rootViewController to the navigation controller after you initialize the navigation controller:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
This is how I'm doing it without storyboards.
The problem was that I didn't call self.navigationController dismissViewControllerAnimated:YES completion:^
After correcting this (see listing below), my WebView was shown as expected.
else if (![versionOfLastRun isEqual:currentVersion])
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSLog(#"%#", self.window.rootViewController);
NSString *url=#"http://devstatwatch.drbsystems.com/releases_mobile.php";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}

GKGameCenterViewController wont dismiss?

I dont get it please help i DONT get a log when i hit done
- (IBAction)loadScores:(id)sender {
GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
// gameCenterController.gameCenterDelegate = self;
[self presentViewController:gameCenterController animated:YES completion:nil];
}
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController*)gameCenterViewController {
NSLog(#"rfffff");
[self dismissViewControllerAnimated:YES completion:nil];
UIViewController *vc = self.view.window.rootViewController;
[vc dismissViewControllerAnimated:YES completion:nil];
}
You have commented the delegates of the GKGameCenterViewController. This means all the delegate methods will not be called like the one that dismisses the view controller. So I suggest you uncomment this line and then run the code and see.

In Modal View, DismissViewControllerAnimated After PresentViewController Not Working

I come across this issue while doing some testing. I have presented a Modal view, called ModalView1. In ModalView1, when a button is pressed, another Modal view, called ModalView2 would be presented using presentViewController. Then I tried dismissing ModalView2 using dismissViewControllerAnimated but it is not working.
Here is the code fragment in button action
- (void) buttonAction: (UIButton*) sender
{
ModalView *ModalView2 = [[ModalView alloc] init];
[self presentViewController:ModalView2 animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
Any help would be much appreciated. Thank you.
It's not clear what you are trying to do. I give you two options:
Presenting ModalView2 and then dismissing ModalView2 (makes no sense to me, but that's what I can read in your question)
- (void) buttonAction: (UIButton*) sender {
ModalView* modalView2 = [[ModalView alloc] init];
[self presentViewController:modalView2 animated:YES completion:^{
[modalView2 dismissViewControllerAnimated:YES completion:nil];
}];
}
Presenting ModalView2 and dismissing ModalView1:
- (void) buttonAction: (UIButton*) sender {
ModalView* modalView2 = [[ModalView alloc] init];
UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[presentingViewController presentViewController:modalView2 animated:YES completion:nil];
}];
}
at time present and dismiss not call so give some time
try this it working me
- (void) buttonAction: (UIButton*) sender
{
[self performSelector:#selector(call) withObject:nil afterDelay:.4];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)call
{
ModalView *ModalView2 = [[ModalView alloc] init];
[self presentViewController:ModalView2 animated:YES completion:nil];
}

SKStoreProductViewController cancel button crash or not working

I have implemented a delegate for the SKStoreProductViewController.
I add that view controller into the key window's view controller.
I have also implemented a dismiss view controller code in the delegate function.
The question seems to be answer in this question.
Modal App Store won't dismiss
Yet, this problem still persist in my situation.
Display function
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
NSString *appURL = [NSString stringWithFormat:#"itms-apps://itunes.apple.com/%#/app/id%#",
[[NSLocale preferredLanguages] objectAtIndex:0], applicationID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
} else {
NSDictionary* dict = [NSDictionary dictionaryWithObject:applicationID forKey:SKStoreProductParameterITunesItemIdentifier];
SKStoreProductViewController *viewCont = [[SKStoreProductViewController alloc] init];
viewCont.delegate = self;
[viewCont loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
{
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if (viewController)
{ [viewController presentViewController:viewCont animated:YES completion:nil]; }
}];
}
Delegate Function
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
if (viewController)
{ [viewController dismissViewControllerAnimated:YES completion:nil]; }
}
The problem is that you must implement
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
if (viewController)
{ [self dismissViewControllerAnimated:YES completion:nil]; }
}
inside the delegate class. If you implement it inside the class whose delegate is presenting the SKStoreProductViewController, it will not work because SKStoreProductViewController will try to call productViewControllerDidFinish: within it's delegate, which isn't implementing that method.
Let me show an example:
#implementation MainViewController
- (void)presentSecondViewController
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[secondViewController setDelegate:self];
[self presentViewController:secondViewController animated:YES completion:nil];
}
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
if (viewController)
{ [self dismissViewControllerAnimated:YES completion:nil]; }
}
#end
#implementation SecondViewController {
id delegate <SKStoreProductViewControllerDelegate>;
}
- (void)setDelegate:(id)delegate
{
_delegate = delegate;
}
- (void)callStoreProductViewController
{
SKStoreProductViewController *viewCont = [[SKStoreProductViewController alloc] init];
viewCont.delegate = _delegate;
[viewCont loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
{
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if (viewController)
{ [_delegate presentViewController:viewCont animated:YES completion:nil]; }
}];
}
#end
So, if I did understand your problem well, you must implement productViewControllerDidFinish: inside your viewController class, as it is the one who is presentig the SKStoreProductViewController.
Hope this helps!
Please try in Delegate function, replace all lines with:
[self dismissViewControllerAnimated:YES completion:nil];
Where does this code reside? You set the delegate to the instance where this code is written, but you are adding the SKStoreProductViewController to the root view.
Have you tried adding the store to self?
[self presentViewController:storeController animated:YES completion:nil];
Thanks for the delegate function. It isn't obvious in the documentation.

Why does instantiateViewControllerWithIdentifier: not allocate more memory?

According to the documentation, instantiateViewControllerWithIdentifier "creates a new instance of the specified view controller each time you call it."
I'm running my app (using ARC) with Instruments' Activity Monitor, and I notice absolutely no difference in memory usage when I use to instantiate a ViewController that was instantiated before.
Why is that? See method goToPreviousPage below:
#implementation CBNavigator
int currentPage = 0;
-(IBAction)goToNextPage{
[self dismissViewControllerAnimated:YES completion:^{
currentPage++;
UIViewController *nextPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"%d", currentPage ]];
[self presentModalViewController:nextPageVC animated:YES];
}];
}
-(IBAction)goToPreviousPage{
[self dismissViewControllerAnimated:YES completion:^{
currentPage--;
UIViewController *previousPageVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"%d", currentPage ]];
[self presentModalViewController:previousPageVC animated:YES];
}];
}
#end

Resources