我尝试在我的应用程序委托中创建弹出警报消息,但它根本没有显示。程序是用Swift 4编写的。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我正在为 Apple Watch 构建跑步锻炼项目,但在实现“自动暂停”功能时遇到问题。委托HKWorkoutSessionDelegate : workoutSession(_:didGenerate:)应该获取系统生成的一些暂停事件。
我遇到的问题是我的会话永远不会启动:暂停事件立即发送给委托。
我的代码:
func workoutSession(_ workoutSession: HKWorkoutSession, didGenerate event: HKWorkoutEvent) {
// press 2 buttons
if(event.type == HKWorkoutEventType.pauseOrResumeRequest) {
print("Detected Press")
if workoutData.isPaused == false {
pauseWorkout()
}
else {
resumeWorkout()
}
}
// Auto-pause
if event.type == HKWorkoutEventType.motionPaused && workoutSettings.autoPause {
print("Auto Pause")
pauseWorkout()
}
if event.type == HKWorkoutEventType.motionResumed && workoutSettings.autoPause {
print("Auto Resume")
resumeWorkout()
}
}
Run Code Online (Sandbox Code Playgroud)
问题出现在“//自动暂停”部分。我错过了什么吗?
在我的 iOS 14 中App,我可以AppDelegate通过执行以下操作来注册旧版:
@main
struct MyApp: App {
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
...
}
}
#if os(iOS)
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
SomeSDK.configure(with: launchOptions)
return true
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
但是,我在文档中注意到您可以制作UIApplicationDelegateAdaptor一个ObservableObject然后它将它注入到EnvironmentObject:
...delegate 将被放置在 Environment 中,并且可以通过使用
@EnvironmentObject视图层次结构中的属性包装器来访问。
我找不到任何有关如何工作的示例。使这项工作作为一个的语法是ObservableObject什么?
AppDelegate在我的 Swift iOS 应用程序中,我在内部异步设置了初始视图控制器didFinishLaunchingWithOptions。现在我添加了一个通用链接,这样如果您转到手机上的相应 URL,它将自动打开应用程序中的正确页面(在本例中,它是一个密码重置 URL,如果正确,将打开密码重置表单在应用程序内)。
我正在处理application(_:continue:restorationHandler:)我continueUserActivity的AppDelegate.
如果应用程序已经在运行,则它可以工作,但否则链接处理会在我的应用程序完成设置初始视图控制器之前发生,并且didFinishLaunchingWithOptions返回时深层链接的视图控制器将被关闭。
在呈现视图控制器之前,如何确保应用程序已完成设置continueUserActivity?didFinishLaunching在设置视图控制器之前,我不能/不应该阻止返回,因此该方法在呈现初始视图控制器之前返回。
我知道我可以检查内部的用户活动字典,didFinishLaunchingWithOptions但continueUserActivity仍然被调用(如果仅在应用程序运行时调用它会简单得多,就像didReceiveRemoteNotification工作原理一样)。是否有一个条件要检查此方法内部以将链接处理推迟到didFinishLaunching?
一些简化的UIApplicationDelegate代码有助于解释:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
doSomeAsyncSetup(
completion: {
presentSomeViewController()
}
)
return true
}
func application(_: UIApplication, continue userActivity: NSUserActivity, restorationHandler _: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL,
let …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController。
我试图使用委托方法来实现,但它没有导航到预期的视图控制器。
这是我迄今为止开发的代码。我在这里使用 xib 设置。
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,CustomTableViewCellDelegate {
@IBOutlet weak var tableView: UITableView!
var customTableViewCell = CustomTableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customTableViewCell.delegate = self
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.tintColor = UIColor(red: 211/255, green: 86/255, blue: 50/255, …Run Code Online (Sandbox Code Playgroud) 试图从app委托"实例化"我的初始视图控制器.我试图从app委托填充NSMutableArray.视图控制器"myMutabelArray"的属性获取在应用程序委托中创建的数组.使用下面的代码,数组不受影响,即使它的计数是4(有四个对象),就像在app委托中创建的那样.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
controller.myMutableArray = mutableArrayCreatedInAppDelegate;
Run Code Online (Sandbox Code Playgroud)
当我从AppDelegate中记录计数时,我得到了4.当我从ViewController中记录计数时,我得到了0.
我也尝试了以下内容,这让我怀疑我没有根据需要获得指向视图控制器的指针.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
[controller.view setBackgroundColor:[UIColor lightGrayColor]];
Run Code Online (Sandbox Code Playgroud) 我希望能够在我的app委托中设置全局函数,以便记录所有未捕获的异常.
有没有办法可以捕获所有未捕获的异常,所以我可以将它们记录到文件中?我已经创建了文件,所以我只需要和方法来调用未处理异常的方法.
我试图在一个uitableview中访问一个刷新控制方法,该方法位于导航控制器内部,这是一个tabbarcontroller,这是我的根,但我很难得到一个精确的句柄.
这是我到目前为止在AppDelegate中的代码,但它不起作用...
UITableViewController *tableView = (UITableViewController *)[[self.tabbarController viewControllers][0] tableView];
[tableView.refreshControl beginRefreshing];
Run Code Online (Sandbox Code Playgroud)
我有5个标签栏项目,我相信我可以通过[0],[1],[2],[3]和我在UITableView中的代码访问(尽管可能无关紧要)...
// Add Refresh Control
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:appDelegate action:@selector(forceDownload) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
[refreshControl release];
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,因为我无法找到任何在线访问这么深的.
objective-c uitabbarcontroller uitableview uinavigationcontroller appdelegate
我的ViewController.swift中有一个按钮:
@IBOutlet weak var exampleButton: UIButton!
Run Code Online (Sandbox Code Playgroud)
我想在AppDelegate中显示/隐藏该按钮,当特定事件发生时(即应用程序进入后台等).我怎样才能做到这一点?
我正在尝试使用我的Xcode项目设置核心数据并遇到一个我似乎无法摆脱的错误.我有一个叫做UserDetails里面的实体StudyHub.xcdatamodeld.我在AppDelegate中的代码:
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "DELETE")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// …Run Code Online (Sandbox Code Playgroud) appdelegate ×10
ios ×8
swift ×7
uitableview ×2
xcode ×2
core-data ×1
healthkit ×1
ios14 ×1
iphone ×1
objective-c ×1
pause ×1
pointers ×1
swiftui ×1
uibutton ×1
watchkit ×1