相关疑难解决方法(0)

目标C:在哪里删除NSNotification的观察者?

我有一个客观的C类.在其中,我创建了一个init方法并在其中设置了NSNotification

//Set up NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(getData)
                                             name:@"Answer Submitted"
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

我在哪里设置[[NSNotificationCenter defaultCenter] removeObserver:self]这门课程?我知道,对于a UIViewController,我可以将它添加到viewDidUnload方法中如果我刚刚创建了一个目标c类,需要做什么?

objective-c nsnotifications ios

101
推荐指数
9
解决办法
8万
查看次数

Swift 3 NSNotificationCenter KeyboardWill显示/隐藏

我有一段代码在Swift 2中工作,我尝试使用xCode将代码更新到最新版本,我修复了除两个问题之外的所有内容

我有这个代码

let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

与此配对

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}
Run Code Online (Sandbox Code Playgroud)

在第一部分我现在得到一个错误说"类型'LoginViewController'没有成员'keyboardwillshow/hide'

我不明白为什么它没有看到下面的方法

有人知道这个问题的解决方案吗?

nsnotificationcenter ios swift swift3

10
推荐指数
2
解决办法
2万
查看次数

How to stick my button to the bottom of the view using SwiftUI?

I am experimenting with SwiftUI and just trying to have a button at the bottom. Right now it is centered. Wondering how I could force a view to stick superview's bottom as you would do in AutoLayout.

struct ContentView : View {
    var body: some View {
        VStack {
            Text("Test")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Thank you!!!

swift swiftui

4
推荐指数
1
解决办法
408
查看次数

Swift 4.2在键盘显示时使tableView的底部向上移动

尽管进行了搜索,但是我对如何最好地解决这个问题感到困惑。

我有一个tableView,底部的单元格是列表的输入,与苹果提醒的工作方式相同。当列表中的项目过多时,键盘将覆盖列表,而我看不到正在输入的内容。

我认为我需要更改表格视图的物理大小,并确保在显示键盘时将其滚动到底部。

有些人说过键盘观察者,但是我发现的大多数代码都已过时,并且在放入Xcode时出错。

NSNotificationCenter Swift 3.0在键盘上的显示和隐藏

这是我能找到的最好的方法,但是我也听说过有关约束的问题,使用UITableViewController而不是嵌入UITableView等等。

这是我到目前为止的代码:

NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)


@objc func keyboardWillShow(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        print("notification: Keyboard will show")
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

}

@objc func keyboardWillHide(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这会使整个视图向上移动,这意味着安全区域(例如导航栏等)在其下方具有TableView。我是否通过这种方法使Navigationview不透明?

keyboard tableview ios swift

2
推荐指数
1
解决办法
1380
查看次数