是否可以从 ViewController 中的 SceneDelegate 访问窗口变量?基本上我需要一个替代方案,UIApplication.shared.delegate.window但找不到任何东西
我在 Xcode11 中创建了一个新的Storyboard 项目,并尝试从这样的自定义类中呈现一个 ViewController(这在旧项目中工作得很好):
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : UIViewController = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as UIViewController
appDelegate.window?.makeKeyAndVisible()
appDelegate.window?.rootViewController?.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
当然,这现在不起作用,因为 AppDelegate 中不再有窗口变量,该变量已移至 SceneDelegate。
错误: Value of type 'AppDelegate' has no member 'window'
您现在如何使这段代码工作?
我正在尝试做的事情:
我试图在用户点击/单击推送通知托盘时显示 ViewController。
编辑:现在我可以通过在 SceneDelegate 中创建一个私有静态变量并在我的自定义类中访问该变量来使其工作:
SceneDelegate.swift
private(set) static var shared: SceneDelegate?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = …Run Code Online (Sandbox Code Playgroud) 有人可以帮我解决这个错误吗?
警告:preg_replace():不再支持 /e 修饰符,请改用 preg_replace_callback
我的原始代码:
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)
所以我这样试过:
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e',
function ($matches) {
foreach ($matches as $match) {
return strtoupper($match);
}
},
strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的错误:
警告:preg_replace_callback():不再支持 /e 修饰符,请改用 preg_replace_callback
我是 Android 开发新手,我正在尝试使用 OkHttp 和 GSon 从 DarkSky API 获取 json 数据。问题是我需要以某种方式从 onResponse 方法返回响应,但它始终为空。
这是我的代码:
class DarkSky : Callback {
private var url: String? = null
private var forecast: Forecast? = null
fun fetchJson(lat: Double, long: Double): Forecast? {
this.url = "${Constants.DARK_SKY_BASE_URL}/${Constants.DARK_SKY_KEY}/$lat,$long?exclude=hourly,flags,offset"
val request = Request.Builder().url(url!!).build()
val client = OkHttpClient()
client.newCall(request).enqueue(this)
return this.forecast // when I debug this its NULL
}
override fun onFailure(call: Call, e: IOException) {
println("error")
}
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) { …Run Code Online (Sandbox Code Playgroud) 有没有办法使用ajax异步填充表单ChoiceType(下拉列表)?
基本上我想做的是:
简短描述:简单的预订系统。
详细描述:
当用户在“preferredDate”中使用日期选择器(或其他)选择日期时,ajax 调用将检查所选日期是否有可用时间段,并用时间填充“availableTimes”ChoiceType(下拉列表)(例如:10:00) 、10:30、11:00、11:30 等)。
可用时间不在数据库中。
我的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('preferredDate', DateType::class, [
'widget' => 'single_text',
'input' => 'datetime',
'format' => 'dd.MM.yyyy',
])
->add('availableTimes', ChoiceType::class, ['required' => false]);
}
Run Code Online (Sandbox Code Playgroud)
我的JS:
$(".reservation-date").change(function () {
$.ajax({
type: "GET",
dataType: "json",
url: "{{URL}}",
data: {},
success: function (data) {
$(".reservation-times").empty();
$.each(data, function (key, value) {
//populate availabletimes dropdown
let disabled = (value.disabled === true ? "disabled" : '');
$(".reservation-times").append('<option ' + disabled + ' …Run Code Online (Sandbox Code Playgroud) 我正在尝试实施 App Tracking Transparency 框架,但遇到了问题,当用户拒绝提示时如何加载非个性化内容。
if #available(iOS 14.5, *) {
ATTrackingManager.requestTrackingAuthorization { (status) in
switch status {
case .denied:
// What do I do here?
//GADMobileAds.sharedInstance().start(completionHandler: nil)
case .restricted, .notDetermined, .authorized:
GADMobileAds.sharedInstance().start(completionHandler: nil)
@unknown default: break
}
}
} else {
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)