I need help , googling also put me at no clue thus i'll post here for guidance and assist.
My situation is that I want the app delegate.m after when you press Ok, I want a simple redirect back to another view . Is that possible?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Daily Vibes"
message:notification.alertBody
delegate:self cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
Is it possible to do ?? After they click the cancel button Okay then they'll just be redirect back to the apps a specific page???
Try this:
Make your app delegate conforms to the UIAlertViewDelegate protocol, and add this code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Okay"])
{
NSLog(#"Okay was selected.");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[(UINavigationController*)self.window.rootViewController pushViewController:vc animated:NO];
}
else if([title isEqualToString:#"Hello"])
{
NSLog(#"Hello was selected.");
}
}
The delegate captures the selected button, checks the title, and acts accordingly. In this case, since you are in the app delegate, you have to do a manual push of the view controller (you aren't in a view controller, so theres no pushing allowed)
Related
I am calling a view controller method which displays UIAlertview, and alertview is displaying but clicked button at index method is not calling. How to fix it?
Following code had used in TestViewController class
- (void) test
{
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:#"OPTUM" message:NSSAlertMessage delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
alert1.tag = 3;
[alert1 show];
}
And calling the above method from app delegate like,
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Notification Received: %#", userInfo);
NSString *NSSMessage = [NSString stringWithFormat:#"%#", userInfo];
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
TestViewController *obj1=[storyboard instantiateViewControllerWithIdentifier:#"View Controller"];
[obj1 handleNotificationMessage: NSSMessage];
}
Thanks in Advance, for any help.
Make sure your calling view controller is declared as a UIAlertViewDelegate:
#interface TestViewController : UIViewController<UIAlertViewDelegate>
And make sure clickedButtonAtIndex has the correct signature:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I'm using AppDelegate method. Why?
When the user click the okay Button, it will force the phone to redirect to a UIControllerView, which is naked to the user eyes.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Daily Vibes"
message:notification.alertBody
delegate:self cancelButtonTitle:#"Wokay"
otherButtonTitles:nil];
[alert show];
}
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Wokay"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Vibes"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
}
I'm getting error of the following:
Warning: Attempt to present <UIViewController: 0x109721840> on <UINavigationController: 0x10921abe0> whose view is not in the window hierarchy!
Is it possible to accomplish it?
Scenario:
User set time via "DatePicker" then when alarm pop via AppDelegate, when the user click Okay. Then the user will be redirected to a page where a harmony message is displayed via UILabel. But the user has only one button on that page "Back". He has to set another time just to view the message via redirecting.
Example pic:
its because window.rootViewController has type UINavigationController, not UIViewController.
you should initialise a NavigationController.
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:yourUIViewController];
self.window.rootViewController = mainNavigationController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Vibes"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
That's the answer...
I have 2 view controllers, ViewController 1(VC1) and 2(VC2). In VC2 I have a back and done button. On clicking back button it goes directly to VC1 and on done it makes an api call and when it gets a response it shows an alert view and clicking ok goes back to VC1. Now when I make a api call a loading bar shows up and disappears when I get response and shows the AlertView. But if during that fraction of second when the loading disappears and AlertView is going to be popped up if I click on back and the view changes to VC1, the alert appears on VC1 and results in a crash.
This is a rare case as no user will purposely try for it but I was wondering if that crash can be managed without disabling the back button. I think there can be other instance such cases like if we are making an asynchronous calls and if the user is allowed to use UI while waiting for response and if any error alert that was suppose to show on one ViewController shows up in another may result in crash since the delegate that alert is referring to is that of the previous view controller. So is there any way to handle this kind of crash efficiently?
//Alert View sample
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[alert setTag:701];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 701)
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
The proper way to fix this problem is to use an instance variable to keep a reference to the alert view.
This instance variable should be set to nil in the alertView:didDismissWithButtonIndex: delegate method.
In the view controller's dealloc method, you call dismissWithClickedButtonIndex:animated: if the instance variable is still set.
Assume _alertView is the instance variable.
Create the alert:
_alertView = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[_alertView setTag:701];
[_alertView show];
Update your existing alertView:clickedButtonAtIndex: method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 701) {
_alertView.delegate = nil;
_alertView = nil;
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
Add:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
_alertView = nil;
}
Add:
- (void)dealloc {
if (_alertView) {
_alertView.delegate = nil;
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
}
I'm having trouble programming an AlertView to push to a new ViewController. I've followed several tutorials and still have not been able to get it function properly. Any help would be appreciated.
- (IBAction)web:(id)sender {
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"You are now entering a website outside of the Company App: Any links or references to other Internet sites (hyperlinks) are provided by Company merely as a convenience to users of this App." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Proceed", nil];
[testAlert show];
[testAlert release];
}
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
}
else if (buttonIndex == 1) {
JBWebViewController *jbweb = [[JBWebViewController alloc] initWithNibName:#"JBWebViewController" bundle:nil];
[self.navigationController pushViewController:jbweb animated:YES];
}
}
You should use UIAlertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
not
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex
and check self.navigationController is not nil.
I need to change to another scene after clicking an alert view button.
Here is my code:
(IBAction)confirmar {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Confirmar"
message:#"Confirma que desea recibir notificaciones en su teléfono móvil"
delegate:self
cancelButtonTitle:#"Cancelar"
otherButtonTitles:#"Si quiero participar", nil];
[alertView show];
}
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex {
if (buttonIndex == 1) {
inscrito *cambia = [[inscrito alloc] initWithNibName:#"inscrito" bundle:nil];
[cambia setTitle:#"inscrito"];
cambia.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
// [self.navigationController pushViewController:cambia animated:YES];
[self.navigationController presentModalViewController:cambia animated:YES];
[cambia release];
NSLog(#"Boton 1");
}
}
I'm trying to change to the UIViewController called "inscrito".
I also added: #import "inscrito.h" at top of the file...
I usually use UIViewController, but it appears you might want to push the new viewcontroller
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Pushing and Popping Stack Items
– pushViewController:animated:
– popViewControllerAnimated:
– popToRootViewControllerAnimated:
– popToViewController:animated:
I think the first code is right, but you need to change this :
[self.navigationController presentModalViewController:cambia animated:YES];
with this :
[self presentModalViewController:cambia animated:YES];
If you don't have navigationController, it won't work.Hope it help