Performing this
self.performSegue(withIdentifier: "segueError2", sender: self)
right after receiving a message using:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
drops a "Attempt to present UIViewController on simpleBrowerVC whose view is not in the window hierarchy!" error message.
Did the same in other places of the same ViewController (inside a: didStartProvisionalNavigation) and worked like a charm.
What I'm doing wrong?
FIXED!
Gave control to my current viewController, performing
UIApplication.shared.keyWindow?.rootViewController = self
UIApplication.shared.keyWindow?.makeKey()
Related
The following code worked properly prior to upgrading to Xcode 7.3;
func myMethod() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(CreateButtonObject.notifyButtonAction(_:)))
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(CreateButtonObject.notifyButtonAction(_:)))
tapGesture.numberOfTapsRequired = 1
}
#IBAction #objc func notifyButtonAction (sender: AnyObject) {
let userInfo:Dictionary<String,AnyObject!>
print("Sender from tap or longpress: \(sender)")
**let button = sender.view as! UIButton**
let soundName = button.currentTitle!
userInfo = ["sender" : sender]
NSNotificationCenter.defaultCenter().postNotificationName(sleepEZButtonActionNotificationKey, object: nil, userInfo: userInfo)
DDLogDebug("CreateButtonObject.notifyButtonAction: Notificaiton! ButtonViewController")
DDLogDebug("CreateButtonObject.notifyButtonAction: Posted Notification sleepEZButtonActionNotificationKey to initiate buttonAction")
DDLogDebug("CreateButtonObject.notifyButtonAction: Button Name: \(soundName)")
DDLogDebug("")
}
But now when I do this in Xcode 7.3 I get the following error on the line with sender.view ;
Ambiguous use of 'view'
followed by a compiler error.
Anyone know what's going on here and how to fix. Can't figure this out. Basically I need to get the UIButton attributes out of the UITapGesureRecognizer object that is created and then activated on a button press. Stuck.
Thanks in advance...
In the declaration func notifyButtonAction (sender: AnyObject), you have typed sender as AnyObject. But an AnyObject doesn't have a view. So in your line sender.view as! UIButton, the phrase sender.view is illegal.
Type sender as a UIGestureRecognizer if that's what it is: func notifyButtonAction (sender: UIGestureRecognizer). A gesture recognizer does have a view, so all will be well.
My function is this and it is called with a "done" UIBarButtonItem.
#IBAction func done(sender: UIBarButtonItem) {
dismissViewControllerAnimated(true, completion: nil)
}
I have read multiple other questions/answers about a deleted instance or an old/extra connection in the Interface Builder or in the View Controller code. However, I only have this one function all properly connected without any extra lingering connections. How do I get rid of the "unrecognized selector sent to instance" error
Thanks in advance!
With the information provided in the question I suspect
There is unwanted connection left. To see that you can do:
1) Go to the IB and select the button.
2) Right click on the button and see all the actions. If you see any unwanted action delete it and try running again.
You can also do it programmatically
Set the target for UIBarButtonItem like this
var b = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "sayHello:"
)
func sayHello(sender: UIBarButtonItem) {
}
If you dont want any parameters in the sayHello function, you can do it
var b = UIBarButtonItem(
title: "Continue",
style: .Plain,
target: self,
action: "sayHello"// Remove the colon
)
func sayHello() {
}
Let me know if it works for you
I have a tableView that has a header, a footer and a prototype cell for dynamic purposes. I have a Sign Out button in the footer but can't figure out how to get it linked up with a function. I setup a segue to go back to a login page but I want an alert box to ask for confirmation first so I need to somehow call a function.
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerCell = tableView.dequeueReusableCellWithIdentifier("Footer") as! MenuTableViewCell
footerCell.logout.targetForAction("logOutButtonClicked", withSender: self)
return footerCell
}
This is what I have now, but upon click it crashes and gives me
'NSInvalidArgumentException', reason: '-[.MenuTableViewCell logOutButtonClick:]: unrecognized selector sent to instance 0x7ffce28615d0'
I have a function called logOutButtonClicked that looks like this...
func logOutButtonClicked(){
let alertView = UIAlertController(title: "Log Out?", message: "Are you sure you want to Log Out?", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
alertView.addAction(UIAlertAction(title: "Log Out", style: .Default, handler: {(alertAction) -> Void in
self.logOut()}))
presentViewController(alertView, animated: true, completion: nil)
}
func logOut(){
performSegueWithIdentifier("goHome", sender: self)
}
Edit:
I've also tried footerCell.logout.addTarget(self, action: "logOutButtonClicked", forControlEvents: .TouchUpInside)
but that gives the same error as well.
You have to create a function called "logOutButtonClicked" since you specified by adding the targetForAction, it would look something like this:
func logOutButtonClicked() {
}
I managed to fix this problem by erasing all of the events and outlets corresponding to the button (the ones you find when you right click on it) and then rewriting the function like so...
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerCell = tableView.dequeueReusableCellWithIdentifier("Footer") as! MenuTableViewCell
footerCell.logout.addTarget(self, action: "logOutButtonClicked", forControlEvents: .TouchUpInside)
return footerCell
}
I'm writing an application that takes data from a QR code scanner. I am just implementing the basics, and have a second view controller that is triggered when a new QR code is detected. The following code is in a custom view controller from RSBarcodes and implemented using CocoaPods.
It won't compile with the error
"Use of undeclared type SecondViewController".
I tried making my SecondViewController public, restarting XCode, and nothing has worked. Any help is appreciated. Thank you!
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! SecondViewController
destinationVC.label = "test"
}
SOLUTION:
Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.
SOLUTION:
Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.
First you have to verify which segue you're pushing to by checking the segue identifier name. Then you have to check if you can even create the destinationVC variable, and then if you can unwrap it using '?' instead of '!'.
override public func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?) {
if segue.identifier == "nameOfMySegue" {
if let destinationVC = segue.destinationViewController as? SecondViewController {
destinationVC.label = "test"
}
}
}
I am encountering a strange error:
2015-04-02 12:20:14.642 test[21167:257788] Failed to connect
(testApp) outlet from (test.AppDelegate) to (NSMenuItem): missing
setter or instance variable
inserted id: 122
I occured when a added a menuItem to a menu and connected a function to it.
I do not know what the Problem is. The app works fine but i don't think it is a smart idea to ignore the error.
What is meant by setter or instance variable? Why is it needed?
UPDATE: Here is the relevant code:
import Cocoa
import Foundation
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
#IBOutlet weak var statusMenu: NSMenu!
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")
statusItem.image = icon
statusItem.menu = statusMenu
// Time for constant repeat
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerRepeat", userInfo: nil, repeats: true)
}
// Method to call the tracking core
func timerRepeat() {
//....
}
#IBAction func frontEnd(sender: NSMenuItem) {
var targetURL : String = NSBundle.mainBundle().resourcePath!
targetURL = targetURL + "/" + "front.app"
let workspace = NSWorkspace()
workspace.launchApplication(targetURL)
}
#IBAction func menuClicked(sender: NSMenuItem) {
NSApplication.sharedApplication().terminate(self)
}
}
You have a broken outlet in your xib file. Usually it happens when you set up an outlet to ivar or property which is later deleted or renamed not using Xcode's rename feature.
Also make sure that your custom view or view controller class is added to your target. (Project => Target Name => Build Phases => Compile Sources). It's possible that a file is in your project but not your target.
This happens because you at one point created an #IBOutlet for a storyboard element. You then later removed the code (reference) from your swift file.
I created an example where I create two extra #IBOutlets (I named them 'correctField' and 'incorrectField'- both are incorrect though) and connected them from my storyboard to my swift file.
I then removed the code from my swift file. This generates the log as shown in the following figure :
To remove this kind of log message, you can do the following:
Go to the 'storyboard' and select the storyboard elements you created connections (#IBOutlets) from.
Open the 'connection inspector' as showed in the figure below
Remove the Referencing Outlets which are incorrect (in my case it is the 'correctField' and 'incorrectField')
Done
This was done in xCode 11