【发布时间】:2011-12-20 11:49:22
【问题描述】:
好的,我对 iOS 开发还是很陌生,所以如果这是一个愚蠢的问题,我深表歉意。
但是,我有一个 AlertView,我从 AppDelegate 调用它,然后在单击警报中的按钮时做出响应。我可以做一个NSLog 并看到方法被调用。但是,它不会将视图推入堆栈。这是我所拥有的样本(我确定这是错误的):
这是在AppDelegate.m:
#import "AppDelegate.h"
#import "ProfileConnection.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
-(void)switchToController:(NSString *)controller animated:(BOOL)animated{
NSLog(@"switching to controller %@", controller);
// maybe we can do a check to see if a subview exists...and then push or pop accordingly.
// switch to the "TableView" view
if( [controller isEqualToString:@"ProfileConnection"]){
NSLog(@"switching to the ProfileConnection view");
ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:@"ProfileConnection" bundle:nil];
[self.navController pushViewController:profile animated:YES];
}
}
-(void)showConnectionFoundAlert
{
NSString *connectFoundMsg = [[NSString alloc] initWithFormat:@"We found someone we'd think you would like to meet: Tony Davis"];
UIAlertView *connectionFoundAlert = [[UIAlertView alloc] initWithTitle:@"Connection Found" message:connectFoundMsg delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Connect", @"View Profile", @"Save For Later", nil];
[connectionFoundAlert show];
//[connectionFoundAlert release];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *alertString = [[NSString alloc] initWithFormat:@""];
if([title isEqualToString:@"Decline"])
{
alertString = @"Declied";
}
else if([title isEqualToString:@"Connect"])
{
alertString = @"Connected";
}
else if([title isEqualToString:@"View Profile"])
{
//alertString = @"Profile Viewed";
//NSLog(@"View Profile is being called");
[self switchToController:@"ProfileConnection" animated:YES];
//UIViewController *profile = [[UIViewController alloc] initWithNibName:@"ProfileConnection" bundle:nil];
//ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:@"ProfileConnection" bundle:[NSBundle mainBundle]];
//UINavigationController *nav = [[UINavigationController alloc] init];
//[nav pushViewController:profile animated:NO];
/*UIViewController *profile = [[UIViewController alloc] initWithNibName:@"ProfileConnection" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] init];
[navigation pushViewController:profile animated:YES];*/
/*
ProfileConnection *profile = [ProfileConnection alloc];
//UIView *current = self.window;
[self.window addSubview:profile.view];
*/
/*
[window addSubview:view1.view];
[window makeKeyAndVisible];
- (void)goToNextPage {
view2 = [ViewController2 alloc];
UIView *current = self.window;
[self.window addSubview:view2.view];
*/
}
else if ([title isEqualToString:@"Save For Later"])
{
alertString = @"Saved It";
}
UIAlertView *alertStr = [[UIAlertView alloc] initWithTitle:@"" message:alertString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
if ([alertString isEqualToString:@""]) {
} else {
[alertStr show];
}
}
@end
这是AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ProfileConnection.h"
@interface AppDelegate : UIResponder <UIAlertViewDelegate, UIApplicationDelegate, UINavigationControllerDelegate> {
UINavigationController *navController;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navController;
-(void)showConnectionFoundAlert;
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
-(void)switchToController:(NSString *)controller animated:(BOOL)animated;
@end
我可以用这个添加视图,但是我失去了我的导航控制器:
ProfileConnection *profile = [ProfileConnection alloc];
[self.window addSubview:profile.view];
您可以看到我已经尝试了几种方法,但我在尝试使用情节提要方法时感到困惑。
此外,如果有帮助的话,ProfileConnection 视图目前是空白的,只有一个标签。
【问题讨论】:
-
如何创建导航控制器?
-
请同时显示您的 ProfileConnection 代码。
-
你的第三个代码 sn-p 来自哪里。在您的 App Delegate 中,
pushViewController是您将某些内容放入堆栈的方式。 -
第三个 sn-p 是一段有效的代码(因为它实际上将视图放在堆栈顶部)但我丢失了导航控制器。
-
导航控制器是在 IB 中使用情节提要创建的。这是我第一次尝试使用故事板,所以我可能会在尝试使用它时感到困惑。
标签: ios iphone uinavigationcontroller appdelegate pushviewcontroller