-1

I have few help tips which are custom subviews which I want to show both in portrait and landscape mode when the app is launched for the first time. Next time the app is launched they don't show up. I want to do this in viewDidAppear method. I wrote the code but not able to figure out how to implement it in landscape mode.
Here's the code for portrait mode in viewDidAppear method:

     for index in subviews{
        index.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("showPopover:")))

        if index == self.hotSpotOne && HelpTipRegistry.shouldShowTip(HelpTipRegistry.names.HomeScreenSearch){

            index.constraintList.append(NSLayoutConstraint(item: index, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -1))
            index.constraintList.append(NSLayoutConstraint(item: index
                , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 16))

            index.helptip(index, parentView: self.view, delay: 0.0)

        }

        else if index == self.hotSpotTwo && HelpTipRegistry.shouldShowTip(HelpTipRegistry.names.HomeScreenEvent){
            index.constraintList.append(NSLayoutConstraint(item: index, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))

            index.constraintList.append(NSLayoutConstraint(item: index
                , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 150))

            index.helptip(index, parentView: self.view, delay: 0.3)
        }

        else if HelpTipRegistry.shouldShowTip(HelpTipRegistry.names.HomeScreenProduct){
            index.constraintList.append(NSLayoutConstraint(item: index, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            index.constraintList.append(NSLayoutConstraint(item: index
                , attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -2))

            index.helptip(index, parentView: self.view, delay: 0.6)

        }

    }

In landscape mode only one subview should be visible instead of 3. I don't know how to implement landscape mode code in viewDidAppear method.

Any help is appreciated! Thanks!

pogbamessi
  • 311
  • 3
  • 17
  • You really need to rewrite your question. The title and the first part makes it sound like you need help with how to "run something just once". But your real issue isn't that, it's how to deal with some code for landscape orientation. Please clarify your question and title. – rmaddy Oct 27 '15 at 21:10

1 Answers1

1

To handle recording that the app was run once, a flag can be stored in the user defaults:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(false, forKey: "appWasRunOnce")
defaults.synchronize()

The flag can be read using:

let defaults = NSUserDefaults.standardUserDefaults()
let appWasRunOnce = defaults.boolForKey("appWasRunOnce")

if appWasRunOnce {
    // Your code here.
}   

To handle the state of the device orientation you can use:

if (UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) {            
    // Handle landscape orientation.
} else if (UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) {
    // Handle portrait orientation.
}
Daniel Zhang
  • 5,778
  • 2
  • 23
  • 28
  • thanks @daniel, i am already using NSUserDefaults. My main problem is how to implement landscape mode in viewDidAppear method that should run only once. – pogbamessi Oct 27 '15 at 21:08
  • Do you just need to detect if the device is in landscape mode? Or are there other special conditions that you are handling? – Daniel Zhang Oct 27 '15 at 21:11
  • detect whether the device is in landscape or portrait mode and then add the code appropriate to the device orientation. – pogbamessi Oct 27 '15 at 21:14
  • Revant, I've added the checks for device orientation that you can use in Swift. – Daniel Zhang Oct 27 '15 at 21:18
  • Revant, I've tested the orientation detection code in `viewDidAppear:` and it works for detecting the orientation. If you are not getting the correct layout then it is a layout issue and you probably have to show both your code and what is happening for someone to be able to help you with it. – Daniel Zhang Oct 27 '15 at 21:30