gpt4 book ai didi

iphone - 切换 ViewControllers,但没有任何反应?

转载 作者:行者123 更新时间:2023-11-29 13:24:22 24 4
gpt4 key购买 nike

我想将切换 ViewController 的代码放在一个名为 SwitchController 的类中。但是没有任何反应。

代码如下:

//AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

RootViewController *root = [[RootViewController alloc] init];
[self.window setRootViewController:root];

[self.window makeKeyAndVisible];
return YES;
}

//SwitchController.h

@interface SwitchController : NSObject

@property (strong,nonatomic) RootViewController *rootViewController;

+(SwitchController *)sharedInstance;
-(void)requestToSwitchViewController:(NSInteger)tag;

@end

//SwitchController.m

#import "SwitchController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation SwitchController

@synthesize rootViewController = _rootViewController;

-(id)init
{
self = [super init];
if (self == nil)
{
_rootViewController = [[RootViewController alloc] init];
}
return self;
}

+(SwitchController *)sharedInstance
{
static SwitchController *sharedSingleton = nil;
@synchronized([SwitchController class])
{
if(sharedSingleton == nil)
{
sharedSingleton = [[self alloc] init];
}
}
return sharedSingleton;
}

-(void)requestToSwitchViewController:(NSInteger)tag
{
switch (tag)
{
case 1:
{
FirstViewController *first = [[FirstViewController alloc] init];
[self.rootViewController presentViewController:first animated:YES completion:nil];
}
break;
......... //Do something else
default:
break;
}
}

@end

//这是self.window.rootViewController------RootViewController.m

//在这个ViewController的 View 中,有一个按钮,按下它会做下面的事情

    - (IBAction)pressed:(id)sender
{
[[SwitchController sharedInstance] requestToSwitchViewController:[(UIButton *)sender tag]];
}

那么,我的代码有什么问题吗?

为什么按下按钮时没有任何反应?

最佳答案

可能有几个问题..

1) 检查你的按钮 socket 是否正确连接到这个方法-(IBAction)pressed:(id)sender;

2)在SwitchController.m中的@synthesize之后添加这一行

static SwitchController *sharedSingleton = nil;

3)像这样改变你的方法

+(SwitchController *)sharedInstance
{
if(sharedSingleton == nil)
{
sharedSingleton = [[self allocWithZone:NULL] init];
}
return sharedSingleton;
}

4) 从-id(init) 方法中删除这一行

// You do not need to re-initialize the root view controller
_rootViewController = [[RootViewController alloc] init];

5) 检查按钮是否有效。在调用此方法之前 requestToSwitchViewController: 检查它记录的内容

更新:

像这样尝试:(为此,您需要在 appDelegate为导航 Controller 创建一个属性并合成它)

switch (tag)
{
case 1:
{
FirstViewController *first = [[FirstViewController alloc] init];
appDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController presentViewController:first animated:YES completion:nil];
}
.....
.........
}

关于iphone - 切换 ViewControllers,但没有任何反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com