我用Swift4的时候Xcode 9给了我
UIApplication.delegate只能从主线程使用
....必须仅从主线程使用
从后台线程组调用的UI API
紫色警告.
我的代码;
var appDelegate = UIApplication.shared.delegate as! AppDelegate
public var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let prefs:UserDefaults = UserDefaults.standard
var deviceUUID = UIDevice.current.identifierForVendor!.uuidString
Run Code Online (Sandbox Code Playgroud)
警告线是;
public var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Run Code Online (Sandbox Code Playgroud)
像这样的另一个警告;
let parameters = [
"tel": "\(self.phone.text!)"
] as [String : String]
Run Code Online (Sandbox Code Playgroud)
给
UITextField.text只能从主线程使用
同样的错误..
我该如何解决?任何的想法 ?
我正在重建一个没有故事板的应用程序,而我遇到的最麻烦的部分是以编程方式导航视图.很少有东西写在那里没有使用故事板,所以找到答案是很困难的.
我的问题非常简单.我有我ViewController和我的SecondViewController想要从前者推到后者.
在AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor = UIColor.whiteColor()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
Run Code Online (Sandbox Code Playgroud)
然后在ViewController.swift:
class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
startFinishButton.setTitle("Begin", forState: .Normal)
startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside)
view.addSubview <*> startFinishButton
}
func moveToSecondViewController(sender: UIButton) {
let vc = SecondViewController()
println(self.navigationController) // returns nil
self.navigationController?.pushViewController(vc, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码从应用程序委托中呈现视图控制器:
- (void)interstitialViewControllerRequestSucceeded:(UIViewController *)interstitialViewController
{
[self.window.rootViewController presentViewController:(UIViewController *)interstitialViewController animated:YES completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)
它将在初始视图控制器上显示插页式广告但不显示其他插页式广告.我希望它显示在连接到导航控制器的每一个上.
如何修改此代码以实现该目标?
所以我在iOS7中有一个完整的工作解决方案,通过appDelegate的didFinishLaunching中的presentViewController显示一个LoginViewController.
基本上我做的是这样的:
UIViewController *backgroundViewController = ...
self.window.rootViewController = backgroundViewController;
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:loginViewController
animated:NO ...]
Run Code Online (Sandbox Code Playgroud)
在iOS8中,我看到了一个跳跃.首先,我看到backgroundViewController然后大约1秒钟左右登录出现.
那么,我怎样才能防止iOS8的这种跳跃?
自从我更新xcode后,我似乎无法改变titleTextAttribute.现在,当我使用以下代码时,我收到此错误:
找不到接受这个提供的参数的重载init
代码appDelegate:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Ubuntu-Light", size: 15), NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud) 新的iTunes连接现在具有分析功能.您可以在网址中指定提供商(pid)ID和广告系列ID(cid).例如
https://itunes.apple.com/fr/app/candy-crush-saga/id553834731?mt=8&pid=1234&cid=My_Campaign
您可以将这些值传递到应用程序中用于各种目的吗?
即使在检查之后,我找不到任何指向的东西:
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)
参考:http:
//www.applift.com/blog/new-era-attribution-analytics.html
我在我的应用程序中进行了演练(入门流程),我想要一个跳过按钮.该按钮位于viewController上,因此我发现移动到另一个viewController的最佳方法是访问app delegate窗口.
但是,它一直让我得到一个AppDelegate.Type没有名为"window"的成员的错误.
@IBAction func skipWalkthrough(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
AppDelegate.window!.rootViewController = RootViewController
}
Run Code Online (Sandbox Code Playgroud)
这种方法有什么问题吗?
提前致谢!
我是ios的新手.我需要从app委托知道当前的视图控制器..我不知道这个,我不知道实现这个.我正在使用此代码来实现此功能,但它返回null值.我按照这个链接 - 从应用程序委托获取当前视图控制器(模态是可能的) 需要帮助.
按照一些指南遇到问题,特别是 http://blog.originate.com/blog/2014/04/22/delinklinking-in-ios/
我正在设置网址方案,它可以很好地从另一个应用程序启动应用程序,但传入主机或网址似乎不应该正常工作.我正在为所有视图布局使用故事板和界面构建器.
该指南在appDelegate中显示了这个openURL:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if([[url host] isEqualToString:@"page"]){
if([[url path] isEqualToString:@"/page1"]){
[self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
}
return YES;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的版本简化,并从其他一些来源迅速,即 获取ViewController的实例从AppDelegate在Swift中我正在跳过url主机的条件,以删除问题中潜在的其他变量.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController
rootViewController.navigationController.popToViewController(profileViewController, animated: true)
return true
}
Run Code Online (Sandbox Code Playgroud)
swift版本导致崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
似乎rootViewController还没有navigationController呢?
uiviewcontroller uinavigationcontroller ios appdelegate swift
我需要支持 iOS 12 和 iOS 13。
我应该在AppDelegate和之间复制代码SceneDelegate吗?
例如:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
self.window = window
}
Run Code Online (Sandbox Code Playgroud)
和
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
self.window = window
return true
}
Run Code Online (Sandbox Code Playgroud)
如果我不这样做,在 1 个版本中我最终会出现黑屏,但是如果我这样做并以 的viewDidLoad方法打印, …
appdelegate ×10
ios ×7
swift ×5
objective-c ×3
analytics ×1
delegates ×1
ios8 ×1
iphone ×1
swift4 ×1
xcode9 ×1