gpt4 book ai didi

ios - 在 Objective C 中切换 View

转载 作者:行者123 更新时间:2023-11-29 11:52:59 26 4
gpt4 key购买 nike

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// UIScreen represents the entire screen. Its bounds method
// returns the rectangle that corresponds to the entire screen
// CGRect is a C struct that used to group four floating
// point values that represent the x and y coordinates of the upper left
// corner, as well as the width and the height.
//

CGRect windowRect = [[UIScreen mainScreen] nativeBounds];
UIWindow *window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor lightGrayColor]];

[self setWindow:window];
UIViewController *vc = [[UIViewController alloc]init];
window.rootViewController = vc;

self.mainView = vc.view;
[window makeKeyAndVisible];

//Window is now visible
//Add created views to the mainView to make them visible.

//Example : add a "Hello World" label
UIView *dealerView=[[UIView alloc]initWithFrame:self.mainView.bounds];

UIView *view1=[[UIView alloc]initWithFrame:self.mainView.bounds];
CGRect labelRect = { 150.0, 150.0, 150.0, 50.0 };
UIButton *button1 = [[UIButton alloc] initWithFrame:labelRect];
[button1 setBackgroundColor:[UIColor blackColor]];
[button1 setTitle:@"ENTER HERE" forState: (UIControlState)UIControlStateNormal];
[button1 addTarget:self action:@selector(enterHereClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setUserInteractionEnabled:YES];

[view1 addSubview:button1];

[dealerView addSubview:view1];

return YES;
}

-(void) enterHereClicked:(UIButton *) button{

CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };

UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view2=[[sender superview]superview];
[view1 removeFromSuperview];

UIView *view=[[UIView alloc]initWithFrame:self.mainView.bounds];

UIButton *button2 = [[UIButton alloc] initWithFrame:labelRect];
[button2 setBackgroundColor:[UIColor blackColor]];
[button2 setTitle:@"Another View" forState: (UIControlState)UIControlStateNormal];
[button2 addTarget:self action:@selector(clickView:) forControlEvents:
UIControlEventTouchUpInside];

[view addSubview:button2];
[view2 addSubview: view];

}
-(void)clickView:(UIButton *) button{

NSLog(@"anand");
UIButton *sender=button;
UIView *view1=[sender superview];
UIView *view3=[[sender superview]superview];
[view1 removeFromSuperview];
CGRect labelRect = { 150.0, 400.0, 150.0, 50.0 };
UIView *view=[[UIView alloc]initWithFrame:labelRect];
[view setBackgroundColor:[UIColor greenColor]];

[view3 addSubview:view];



}



I have edited the code as per you advise. But still not working.

我正在创建一个新 View dealerview 并从中添加和删除 subview 。我要去哪里错了,但仍然无法弄清楚。 看起来我在 -(BOOL) 应用程序方法中出错了。

最佳答案

在应用委托(delegate)中做这种事情是非常不典型的。最好在 UIViewController 的子类中执行此操作,并将该子类的实例添加为 rootViewControllerUINavigationController 您在 didFinishLaunchingWithOptions 中设置为 key 和可见。

您也可能不想从 UIViewController 的 super View 中删除 Root View 。目前,您关心的是为按钮创建一个操作,从 View 层次结构中删除其父 View vc.view 。问题是 view1self.mainView 是一样的:

UIView *view1=[sender superview];
[view1 removeFromSuperview];
[self.mainView addSubview:view];

您正在从 View 层次结构中删除一个 View ,然后将一个 View 添加到刚刚删除的 View 中并希望看到它。

您应该有一个 View 充当 button1 的容器,另一个 View 充当 button2 的容器,但都添加到 self.mainView 或从 ojit_code 中删除。

关于ios - 在 Objective C 中切换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40335054/

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