Check top view controller swift is launch screen site stackoverflow.com năm 2024

My UIViewControllers are listening for NSNotifications - even if when they are not displayed [ie not shown]. So I could have 10 UIViewController in the background observing NSNotifications from

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

0. When an

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

1 is posted and received by the UIViewController, I'd like to find out if it is currently being shown. If it is not, I will just set a boolean so that it will processed when the View is presented. If it currently being display, I will do more things like update tables immediately, and so forth...

iDev

23.3k7 gold badges61 silver badges85 bronze badges

asked Oct 9, 2012 at 20:29

4

You need to check if your viewcontroller is on top of the stack of navigationcontroller's viewcontroller array. A sample code is,

if [self.navigationController.topViewController == self] {
    //the view is currently displayed
}

You can use this inside the

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

3 method to check whether the current view is visible.

answered Oct 9, 2012 at 22:26

iDeviDev

23.3k7 gold badges61 silver badges85 bronze badges

2

Check to see if it's attached to the window. If it's not

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

4 it's in hierarchy which is attached to the screen [of course it could be off the bounds of the screen, covered by some other view or have the hidden flag set]

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

answered Oct 9, 2012 at 20:56

DBDDBD

23.2k12 gold badges62 silver badges84 bronze badges

4

You can use flags in

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

3 and

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

6 methods for this.

Rob

423k74 gold badges809 silver badges1063 bronze badges

answered Oct 9, 2012 at 22:34

Why don't you remove the notification listener in viewWillDisappear and add it in viewWillAppear?

Edit: misread his question, sorry.

Suggested answer: set your own flag [BOOL] in viewDidDisappear and viewDidAppear.

answered Oct 9, 2012 at 20:37

yufyuf

3,0923 gold badges20 silver badges18 bronze badges

5

Specify title to each ViewController and then get the title of current ViewController by the code given bellow.

NSString *currentController = self.navigationController.visibleViewController.title;
Then check it by your title like this
if[[currentController isEqualToString:@"myViewControllerTitle"]]{
    //write your code according to View controller. 
}

tkanzakic

5,49916 gold badges34 silver badges41 bronze badges

answered Aug 19, 2013 at 9:15

Neel KamalNeel Kamal

2313 silver badges2 bronze badges

I think that checking of

if [myViewController.view.window] {
  // I'm attached to the window
} else {
  // not attached to the window
}

7 should works.

answered Oct 9, 2012 at 20:45

4

It's too late to replay on this question.

To check the instance of a UIViewController is currently on the top of the screen or to check if it is showing on screen, you can put a check like:

How to check top view in Swift?

extension UIViewController { /// Top most ViewController func topMostViewController[] -> UIViewController { if self. presentedViewController == nil { return self } if let navigation = self.

How do you check if ViewController is presented or pushed?

Here is solution that addresses the issue mentioned with previous answers, when isModal[] returns true if pushed UIViewController is in a presented UINavigationController stack. self. navigationController != nil would mean it's in a navigation stack.

How to show ViewController as popup in iOS Swift?

Pop up UIViewController on Click of UIButton in Swift 4 [POP UP Controller].

Take a Two UIViewController like that:.

Here is “POP” is UIbutton , connect firstView Controller to another by Model Popover . a: Select Button. b: go To “Connection Inspector” ... .

Now Its time to write the code in 1st viewcontroller..

How to pass data between view controllers in Swift?

Define a protocol that declares the necessary methods to pass the data. Create a delegate property in the destination view controller and set it to the source view controller. Implement the protocol methods in the source view controller. Call the delegate methods from the destination view controller and pass the data.

Chủ Đề