我正在使用一些我需要重构的代码.视图控制器充当两个其他视图控制器的容器,并将在它们之间进行交换,如下面的代码所示.
这可能不是最好的设计.可能不需要以这种方式交换视图控制器.我明白那个.但是,当我使用这段代码时,我想进一步了解addChildViewController调用会发生什么.我无法在Apple的文档或相关问题中找到答案(这可能表明设计需要改变).
具体来说 - 容器视图控制器如何处理要求添加已添加的子视图控制器的情况?它是否认识到它已经添加了该视图控制器对象?
例如,如果下面的代码在方法内 - 并且该方法被调用两次...
[self addChildViewController:viewControllerB];
[self.view addSubview:viewControllerB.view];
[viewControllerB didMoveToParentViewController:self];
[viewControllerA willMoveToParentViewController:nil];
[viewControllerA.view removeFromSuperview];
[viewControllerA removeFromParentViewController];
Run Code Online (Sandbox Code Playgroud)
谢谢,加文
我有一个单独的视图应用程序,并希望在按右侧的导航栏按钮时显示一个新的ViewController.我通过以下代码调用此VC:
- (IBAction)createEntryButton:(id)sender {
CreateEntryViewController *vc2 = [[CreateEntryViewController alloc] init];
[self presentViewController:vc2 animated:TRUE completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
然而,这个动画vc2从底部带来了根据我的UI看起来反直觉的内容.所以我的问题是:
如何使用presentViewController从右侧而不是底部显示我的vc2?
谢谢.
在我的程序中,我有一个UIViewController子类MyViewController和该视图控制器的两个子类.
我希望他们都使用相同的xib,所以我启动它们
SubClass *SC = [[SubClass alloc] initWithNibName:@"MyViewController" bundle:nil];
[self presentModalViewController:SC animated:NO];
[SC release];
Run Code Online (Sandbox Code Playgroud)
SubClass是MyViewController的子类,它是MyViewController的子类UIViewController.在MyViewController.xib,我有文件的所有者设置为MyViewController.
如果我只有两个子类,我可能只是复制xib但我计划有许多子类,都使用相同的xib.
大家好我是iOS开发的新手,目前我遇到了一些问题,如果用户没有登录,我正在尝试以模态方式显示登录表单.
我正在使用NSUserDefaults存储一个id,我只是检查它是否存在.如果它没有,那么我希望UIViewController出现.
到目前为止,我在UINavigationController中有以下代码,它位于UITabViewController中,我试图让UIViewController出现在第一个UINavigationController(被选中的那个)之上,在动画发生的那一刻但是只有一个即使登录屏幕已经创建了所有相关的文本框等,也会出现黑屏.如果我将此登录屏幕设置为要加载的初始视图,则会加载正常.
这是出现在UINavigationController的First viewcontroller中的代码.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs stringForKey:@"id"]){
LoginViewController *loginController = [[LoginViewController alloc] init];
[self presentModalViewController:loginController animated:YES];
} else {
[self loadCoffeeUserOrders:[prefs stringForKey:@"id"]];
}
Run Code Online (Sandbox Code Playgroud)
模态视图正在加载,但目前它只是显示黑色,我尝试创建一个新的登录屏幕,同样的事情发生:只有一个黑屏.
希望有足够的信息让某人了解可能发生的事情,我对iOS开发很新,所以如果我犯了一些错误,那么知道我哪里出错了会很高兴.
谢谢
我一直在试图弄清楚如何在我的Spritekit场景中隐藏和显示iAd.目前我的设置如下:
ViewController.h
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAD.h>
@interface ViewController : UIViewController <ADBannerViewDelegate> {
ADBannerView *adView;
}
-(void)showsBanner;
-(void)hidesBanner;
@end
Run Code Online (Sandbox Code Playgroud)
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <iAd/iAD.h>
#import "MyScene.h"
#import <SpriteKit/SpriteKit.h>
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
self.canDisplayBannerAds = YES;
adView = …Run Code Online (Sandbox Code Playgroud) 我想通过按下"感觉幸运"按钮切换到第二个视图.当我试用我的模拟器并按下它时,它就会变成黑屏.我怎么能解决这个问题?
@IBAction func FeelingLuckyPress(sender: UIButton) {
let secondViewController:SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个包含WkWebView的弹出模式视图的应用程序.当我想在此模态视图中上传图像并且出现"照片选择"时,模态视图只会将其解除回发射它的视图控制器.
我怎么能防止这种情况?
import UIKit
class PostWindow : UIViewController {
@IBAction func close(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// do stuff here
let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 70, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "https://m.facebook.com/")!))
self.view.addSubview(myWebView)
self.title = "News Feed"
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
UIApplication.sharedApplication().statusBarHidden = false
/*let addButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search,
target: self,
action: #selector(self.openSearch(_:)))
self.navigationItem.setRightBarButtonItems([addButton], animated: true)*/
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Run Code Online (Sandbox Code Playgroud)
}
谢谢!
所以我有一个视图控制器,我显示如下:
func showProfileForTrainer(trainer: Trainers) {
let viewPTProfileVC = ViewPTProfileVC()
viewPTProfileVC.trainer = trainer
self.navigationController?.pushViewController(viewPTProfileVC, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
但是当试图忽视这个观点时,我无法让它发挥作用.它在导航栏中有一个后退按钮,功能很好,但是当试图通过按钮关闭视图时,它什么都不做.我目前尝试过:
func handleMessageTrainer() {
dismiss(animated: true) {
print(1)
self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
}
self.dismiss(animated: true) {
print(2)
self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
}
navigationController?.dismiss(animated: true, completion: {
print(3)
self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
})
self.navigationController?.dismiss(animated: true, completion: {
print(4)
self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
})
print(5)
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我尝试过各种各样的方法而且都没有工作,而控制台只是输出5.
令人沮丧的是,在我的应用程序的其他地方,我以与开头所示相同的方式呈现了一个视图,并且它被解雇了 dismiss(animated: true) { ... }
知道为什么它不能在这里工作吗?
我正在尝试创建一个与共享扩展一起使用的自定义视图控制器.一切正常,但我不知道如何解雇自定义视图控制器.基本上我从safari启动我的共享扩展,并希望完全忽略它并返回到safari视图.我知道这应该不难,但我是新手分享扩展.以下是我的基本代码.谢谢.短发
//
// ShareViewController.swift
// ShareExtension
import UIKit
import Social
import MobileCoreServices
class ShareViewController: UIViewController {
private var url: NSURL?
@IBAction func backButton(_ sender: Any) {
print("back button pressed")
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
private func getURL() {
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? …Run Code Online (Sandbox Code Playgroud) 我正试图在我的游戏上创建一个暂停屏幕.我在故事板中添加了一个'PauseScreen'viewController,故事板ID和Restoration ID设置为"PauseScreenID",并移动到暂停屏幕我已在"GameScene"中创建了该功能:
func pauseSceenTransition(){
let viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") as UIViewController
let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)
currentViewController.window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
但是当它被调用时,我得到错误:
警告:尝试在< AppName .StartScreenViewController:0x7fae61f79980>上显示< AppName .PauseScreen:0x7fae61fe5ff0>,其视图不在窗口层次结构中!
"StartScreenViewController"是我的开始屏幕的视图控制器,是初始视图控制器.然后转到"GameScene",这是"PauseScreen"需要去的地方.如果我将初始视图控制器设置为"GameViewController"/"GameScene",它可以工作,所以我认为我需要更改第二行:
let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)
Run Code Online (Sandbox Code Playgroud)
所以它在"GameViewController"上呈现"PauseScreen",而不是"StartScreenViewController",但我不知道该怎么做.
viewcontroller ×10
ios ×7
swift ×5
objective-c ×3
iphone ×2
dismiss ×1
iad ×1
ios5 ×1
modal-view ×1
share ×1
sprite-kit ×1
xcode ×1
xcode6 ×1