I have a window DeviceNotFound in my AppDelegate class.
class AppDelegate: NSObject, NSApplicationDelegate {
let deviceNotFoundWindowController = NSStoryboard(name: "Main", bundle:nil).instantiateController(withIdentifier: "DeviceNotFoundWindowController") as! DeviceNotFoundWindowController
func test() {
if let loadDataVc = NSStoryboard(name: "Main", bundle:nil).instantiateController(withIdentifier: "LoadDataViewController") as? LoadDataViewController
{
self.deviceNotFoundWindowController.contentViewController!.presentViewControllerAsSheet(loadDataVc)
}
}
}
When I call test function to show another ViewController as Sheet, it displaying like window. My DeviceNotFound window not blocked. I can move it:
Oh, found issue. Because my initial window controller != deviceNotFoundWindowController that i get from nib.
Related
I am trying to navigate to the ClassroomViewController once I receive the push notification. I have put a break point, and it hits all the lines, but it does not show the ClassroomViewController. I am wondering what I am missing in my current implementation.
I added identifier on the ClassroomViewController on storyboard.
AppDelegate
public var keyWindow: UIWindow? {
return UIApplication.shared.windows.first { $0.isKeyWindow }
}
Helper Method which does not work. I am wondering why the following approach does not work.
private func notificationToNavigate() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let topViewController = appDelegate.keyWindow?.rootViewController
else { return }
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let cVC = storyboard.instantiateViewController(withIdentifier:"ClassroomViewController") as! ClassroomViewController
topViewController.navigationController?.pushViewController(cVC, animated: true)
}
Helper Method which works
private func notificationToNavigate() {
guard let window = UIApplication.shared.keyWindow else { return }
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let cVC = storyboard.instantiateViewController(withIdentifier:"ClassroomViewController") as! ClassroomViewController
window.rootViewController = UINavigationController(rootViewController: cVC)
window.makeKeyAndVisible()
}
If topViewController is indeed the key window's root view controller, then it cannot have a navigation controller; if pushing is possible at all, then it must be a navigation controller. So you would say
(topViewController as? navigationController)?.pushViewController...
On the other hand, if topViewController is not a navigation controller (so that the above fails), then pushing is simply impossible and you need to think of something else to do.
I want my app to check at start conditionaly if a variable is correct or not. Based on that it should either go to an intro screen (where he can select a variable in my case select a team) or it should start the main view. After searching i found this code and edited it.
But there still seems to be problems. First of all I dont have two identifier. The Intro has one but not the main view. My main View is called WeatherViewController and the Intro screen is called FirstScreenViewController.
I also added a picture of my Main.storyboard.
I also googled a lot about conditional UINavigationController but I can only understand with a video and did not found a video about it.
I tried to use the code from here.
var id = hello ? "goToIntro" : "???"
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let WeatherViewController: UIViewController = mainStoryboard.FirstScreenViewController(withIdentifier: WVC has no identifier??) as UIViewController
self.window?.rootViewController = WeatherViewController
self.window?.makeKeyAndVisible()
if hello {
self.performSegue(withIdentifier: "goToIntro", sender: self)
} else {
/here nothing should happen. It should open the Main View
self.performSegue(withIdentifier: "???", sender: self)
}
Simply repeat the steps you did for creating the segue goToIntro.
Add a new UIViewController (MainViewController) in your storyboard.
Add a segue and give it an identifier.
Provide the identifier value in the else condition of the code that you've shown in your post to perform the segue to the UIViewController that you've just created.
The final code should look something like this:
var id = hello ? "goToIntro" : "goToMain"
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let weatherViewController = mainStoryboard.instantiateInitialViewController()
window?.rootViewController = weatherViewController
window?.makeKeyAndVisible()
weatherViewController.performSegue(withIdentifier: id, sender: self)
You don't need the if else block since the id is already determined using the ? ternary operator.
I already checked most of the answers to this question but mostly are for Cocoa Touch. I need to implement this in my appdelegate for mac application.
Once user is logged-in he will be redirected to the main screen else go to login screen.
let controller:NSWindowController = NSWindowController()
let viewController:NSViewController
let storyboard = NSStoryboard.init(name: "Main", bundle: nil)
let stringLoginStatus = NSUserDefaults.standardUserDefaults().objectForKey(Constants.Key_LoginStatus) as? String
if stringLoginStatus != nil
{
if stringLoginStatus == "true"
{
viewController = storyboard.instantiateControllerWithIdentifier("Channel") as! NSViewController
}
else{
viewController = storyboard.instantiateControllerWithIdentifier("ViewController") as! NSViewController
}
}
else{
viewController = storyboard.instantiateControllerWithIdentifier("ViewController") as! NSViewController
}
controller.window?.contentViewController = viewController
controller.window?.makeKeyAndOrderFront(self)
It shows and error as controller is not being initialized.
try this code to change your view controller:-
UIApplication.appWindow.rootViewController = UIStoryboard.main.instantiateViewController(withIdentifier: "customViewControl")
//MARK: Make UIApplication extension to get appWindow
extension UIApplication {
class var appWindow: UIWindow! {
return (UIApplication.shared.delegate?.window!)!
}
}
//MARK: Make Storyboard extension to get main screen
extension UIStoryboard {
class var main: UIStoryboard {
let storyboardName: String = Bundle.main.object(forInfoDictionaryKey: "UIMainStoryboardFile") as! String
return UIStoryboard(name: storyboardName, bundle: nil)
}
}
good morning,
i have this class:
import Cocoa
class PopOverDetails: NSViewController {
var dID = String()
#IBOutlet weak var txtEmail: NSTextField!
public func fillDetails (ID:NSManagedObjectID) {
print("=== fillDetails ===")
print(ID)
dID = "\(ID)"
}
override func viewDidLoad() {
print("=== viewDidLoad ===")
print(dID)
}
}
i call the function from another view controller
let Controller = PopOverDetails()
Controller.fillDetails(ID: list[0].objectID)
al works fine, but the problem is:
in my function fillDetails, i get the objectID Value which i "send" from the view controller.
but i can't work with this value in the viedidload because it is empty.
this is my output:
=== fillDetails ===
0x40000b <x-coredata://7E006435-3E05-41F9-A3E4-CB8179A319A9/list/p1>
=== viewDidLoad ===
where is my mistake? :)
UPDATE
let vcDetails = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "PopoverDetails") as! NSViewController
popover.contentViewController = vcDetails
popover.show(relativeTo: tblView.rect(ofRow: tblView.selectedRow) , of: tblView, preferredEdge: .maxX)
}
First declare variable in PopOverDetails i.e."id",then
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "your_identifier") as! PopOverDetails
secondViewController.id = list[0].objectID
self.navigationController?.pushViewController(secondViewController, animated: true)
Now you can get that variable in PopViewController's class
I currently instantiate the storyboards programmatically like so:
var iphone35StoryBoard:UIStoryboard = UIStoryboard(name: "iphone35", bundle: nil)
var initialViewController:UIViewController = iphone35StoryBoard.instantiateInitialViewController() as! UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
This is my first time working with push notifications, so I'm unsure as the best practice, but what i'm trying to do is in didReceiveRemoteNotification and while the app is in an active state(foreground), I would like to be able to determine what view the user is currently on to determine if i should A) Call a local notificaiton or B) update the view if the push notification corresponds to the visible view.
Using various other SO related questions, I have tried to access the visibleviewcontroller using these variations of code:
let navigationController = application.windows[0].rootViewController as! UINavigationController
1)
let visibleController: AnyObject? = navigationController.visibleViewController
if visibleController is ProfileViewController {
println("this works")
}
and
2)
let viewControllers: [UIViewController] = navigationController.viewControllers as! [UIViewController]
for vc in viewControllers {
if vc is ProfileViewController {
println("this works")
}
}
3)
if let wd = self.window {
var vc = wd.rootViewController
if(vc is UINavigationController){
vc = (vc as UINavigationController).visibleViewController
}
if(vc is ProfileViewController){
println("this works")
}
}
the problem i'm running into is that I can only obtain the rootviewcontroller and never the visible view controller. The rootviewcontroller in this case is a login screen, and despite me being on the profileViewController, i can only obtain the loginviewcontroller. Is this the best method for dealing with push notifications?
Move to destination view controller from storyboard when push notification will came
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var destViewController : destVC
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("destVC") as! destVC
var navigationController = UIApplication.sharedApplication().keyWindow!.rootViewController as! UINavigationController
navigationController.pushViewController(destViewController, animated: true)
This is working for me from appdelegate.
Use notification center for more stuff :
NSNotificationCenter.defaultCenter().postNotificationName("name", object: nil, userInfo:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "method:", name: "name", object: nil)
func method(notification: NSNotification) {
//Do your stuff.
}
Passing data with notification center
NSNotificationCenter.defaultCenter().postNotificationName("name", object:nil, userInfo:["message":"Unable to add \(homeName) Home"])
func method(notification: NSNotification) {
//Do your stuff.
println(notification.userInfo)
}