Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控制器或令牌中的IOS AppDelegate变量_Ios_Swift - Fatal编程技术网

控制器或令牌中的IOS AppDelegate变量

控制器或令牌中的IOS AppDelegate变量,ios,swift,Ios,Swift,我完全是{新手/新手}应用程序开发者,如果我问任何愚蠢的问题,请容忍我 当我的应用程序运行时,我试图在我的故事板上显示令牌。如果你查一下我的密码。我已经创建了委托变量var tokenVal=”“,当我运行应用程序时,我的故事板上没有任何值,尽管它在控制台中有打印标记 第二个问题显示警告,尽管它正在编译和运行 这是我的问题AppDelegate.swift代码 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIAp

我完全是{新手/新手}应用程序开发者,如果我问任何愚蠢的问题,请容忍我

当我的应用程序运行时,我试图在我的故事板上显示令牌。如果你查一下我的密码。我已经创建了委托变量
var tokenVal=”“
,当我运行应用程序时,我的故事板上没有任何值,尽管它在控制台中有打印标记

第二个问题显示警告,尽管它正在编译和运行

这是我的问题AppDelegate.swift代码

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var tokenVal = ""
    var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    registerPushNotifications()

    return true
}


func registerPushNotifications() {
    DispatchQueue.main.async {
        let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
    }
}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != UIUserNotificationType() {
        application.registerForRemoteNotifications()
    }
}




func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }

    tokenVal = token
    print(token)
}



func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Registration failed!")
}


func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

这是一个简单的操作顺序问题。在视图控制器的
viewDidLoad
运行后,将设置应用程序代理的
tokenVal


(一般来说,使用一个类的属性作为另一个类的“下降”来获取值是一个笨拙的策略。如果另一个类需要此值,请将此值提供给另一个类。)

先生,谢谢您的投票和回答,因为我告诉您我是新的,我不知道这么多事情,这就是为什么我在这里寻求与您一样的上师的帮助。我的知识非常有限。想再多弄点。再次感谢…使用Apple的Master Detail应用程序模板创建一个新项目。查看MasterViewController如何通过说出
controller.detailItem=object
向DetailViewController发送值。现在看一下DetailViewController是如何配置为响应的,这样无论何时(在
viewDidLoad
之前或之后)界面都会更新。
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var TokenLb: UILabel!
    @IBOutlet weak var tokenVarVal: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        //let aVariable = appDelegate.
        let bVariable = appDelegate.tokenVal
        tokenVarVal.text = bVariable
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}