Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
hello,

Using Xcode 8 I am learning how to program an app. I watched a youtube series How To Make An App by codewithchris; but it leaves many questions unanswered. I am quite the novice with regards to the Swift programming language though I can understand most of it due to a basic knowledge of C++

I wonder how to arrange my app so that there will be a screen with a certain number of UIViews with few Labels, but when the program has allowed the user to accomplish the intended task how to transition to a new screen that has a different number of UIViews and Labels.

Would this be done with the View Controller? How would I connect the first screen to the next screen using the View Controller? Is there some piece of code to write or something that is done within the Main.Storyboard?

thanks for your time and patience - my knowledge of Xcode 8 is very slight yet.
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
I would look for a tutorial about transitions, Segue and adding views. Basically you add a new view and a segue to control between them

https://www.appcoda.com/custom-segue-animations/

It's pretty common with larger projects to add new views, so just about any tutorial should be able to do step-by-step.

Another option is to do a master-detail program. It comes as a stock template in Xcode. When Xcode starts, select a master-detail program and that'll have a starting place you can look at.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
it there a way to make the segue delay the transition to the new screen/view controller by a certain time? Say 5 seconds?
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
it there a way to make the segue delay the transition to the new screen/view controller by a certain time? Say 5 seconds?
Yes, in fact you can get very fancy with it.

I'd look at animations, they usually have a delay to cause certain effects. You can setup a timer or have work start after a delay.

There's tons and tons of fancy things you can do.

I'd start by looking at how iOS handles animations and play around with the delay stuff in there.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
well, I'm experiencing trouble finding what I need. I don't really want an animation while seguing from one screen/view controller to the next. I want simply for the user to accomplish a task by pressing a button, to see an output on the initial screen concerning operations related to that button push and then to change the screen without fanfare after a few moments. Any ideas what type of function that might be? I'm not sure now that seguing is the appropriate way to transition to the new screen.
 

1458279

Suspended
May 1, 2010
1,601
1,521
California
well, I'm experiencing trouble finding what I need. I don't really want an animation while seguing from one screen/view controller to the next. I want simply for the user to accomplish a task by pressing a button, to see an output on the initial screen concerning operations related to that button push and then to change the screen without fanfare after a few moments. Any ideas what type of function that might be? I'm not sure now that seguing is the appropriate way to transition to the new screen.
It's not required to have an animation in order to have a delay. You can also have a nil animation, an animation that does nothing, that way you get just the delay.

The delay is the key point, it's used in animations or you can write one that works solo.

The function would be delay.

You can also call the action of bringing up the new screen after the delay, some have a delay parameter built in (IIRC).

Another option is to create a timer.
 

bjet767

Suspended
Oct 2, 2010
967
319
it there a way to make the segue delay the transition to the new screen/view controller by a certain time? Say 5 seconds?

What I see is you are beginning the never-ending journey of learning the ins and outs of the iOS api, it's huge!

First, if you know C++ then Objective C will make more sense to you than Swift. Swift is more like the newer and popular languages in their syntax than C++, but a competent coder can make the transition easily.

Second every iOS view has "animations" as a part of their DNA, you just don't know that yet. To get a delay in how a view "animates" to the next view you will have to learn how to code the segue and associate view rather than rely on the Storyboard itself. Peronally I like to use Storyboards as much as possible because it eliminates a whole lot of typing of code and allows me to concentrate on the intent of the app more.

The recommendation I always give is the same that was given to me decades ago when I started down the long roaad of development, "find a project and build it."

BTW to change animation time you will have to change the UIViewController's view (from your storyboard layout) by calling:

ObjectiveC:
+ (void)animateWithDuration: (NSTimeInterval)duration: (animations void (^)(void))animations;

Swift:
class func animate(withDuration duration: TimeInterval, animations: () -> Void)

Look it up in the xCode documentation.


Now you have to figure out where this change are made before the view appears.


enjoy!
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
it there a way to make the segue delay the transition to the new screen/view controller by a certain time? Say 5 seconds?

There's all sorts of ways. You can use an NSTimer, performSelector after delay, etc. KarlJay and bjet are also correct you can use a 5 second animation who's completion block would get called afterwards. here's my personal favorite:

Code:
let waitQueue = dispatch_queue_create("waitQueue", nil);

dispatch_async(waitQueue, ^{
   NSThread.sleepForTimeInterval(5);

   dispatch_async(dispatch_get_main_queue(), ^{
      //perform your segue here
   });
});
 

bjet767

Suspended
Oct 2, 2010
967
319
I 'm not sure if the thread starter wanted to delay the segue or slow the animation. The code you offer just makes the user wait for the segue to begin while the animation delay (built into the framework) will slow how fast it happens. I believe the latter is what is desired.

Delaying the actual segue from beginning will frustrate the user and cause them to possibly make an extra tap or two to get it started. If this is the kind of delay desired a spinning wait icon may be needed.
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
I 'm not sure if the thread starter wanted to delay the segue or slow the animation. The code you offer just makes the user wait for the segue to begin while the animation delay (built into the framework) will slow how fast it happens. I believe the latter is what is desired.

Delaying the actual segue from beginning will frustrate the user and cause them to possibly make an extra tap or two to get it started. If this is the kind of delay desired a spinning wait icon may be needed.
That is correct, but I didn't see him ask how to make the transition animation take longer. He just asked how to delay the segue for some time interval, ie. 5 seconds.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
I wonder if this snippet of code would serve to accomplish my goal?
Code:
let delay = 1 * Double(NSEC_PER_SEC)
            let time = dispatch_time(dispatch_time_t(DispatchTime.now), Int64(delay))
            dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
              
                self.performSegueWithIdentifier("showSecondViewController", sender: self)        }
Should it be placed inside the IBaction outlet code? or where? Also Xcode gives a warning a that it cannot invoke initializer for dispatch_time_t with an argument list of type (() -> DispatchTime) What is DispatchTime.now and how do I use it properly?

I regret that I don't understand what it's trying to communicate to me. thanks for all your help and patience
 
Last edited by a moderator:

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
bump, sorry for my obtuseness but I'm still confused about how to implement a delay before a segue - after a button is pushed.

I don't understand where the code should be placed or why my proposed code does not work. I'm not asking anyone to write code for me, simply to help me better understand the code classes and functions and where to place them.

I'm still very ignorant about Swift 3, is there no way to place the delaying code in the same code for the button or is it better to put the code in some global manner related to the view controller itself? If so, where should it be placed.
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
It's very easy and simple. Inside of the IBAction function that gets called when the button is pressed, you create a background thread, sleep for X second (5 in this example), and then hop back on to the main thread and perform the segue.

Code:
@IBAction func buttonPressed(sender: UIButton)
{
     let waitQueue = dispatch_queue_create("waitQueue", nil);
     
     dispatch_async(waitQueue, ^{
       NSThread.sleepForTimeInterval(5);
       
        dispatch_async(dispatch_get_main_queue(), ^{
           //perform your segue here
           self.performSegueWithIdentifier("testSegue", sender: self);
        });
     });
}

Another option is to use an NSTimer.

Code:
@IBAction func buttonPressed(sender: UIButton)
{
     NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats: false);
}

func timerFired()
{
     self.performSegueWithIdentifier("testSegue", sender: self);
}
 

bjet767

Suspended
Oct 2, 2010
967
319
The prize:

Not to be rude, but what is your intent for causing a delay between button push and the segue being activated?

What you are doing is confusing the user by causing them to believe they have not pushed the button correctly or there is a problem with your app. A good practice would be to inform your user in someway there is a built in delay. Use a spinning indicator or even a custom animation or message and it could be as simple as posting in text , "Standby..."

Most of the battle in making a great app is the user interface.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
okay! thanks to everyone for their input, but I have found some code that works and discovered that I overlooked something to facilitate success:

Code:
  func delay(delay:Double, closure:@escaping ()->()) {
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            closure()
        }
    } 
delay(delay: 4) {
            self.performSegue(withIdentifier: "firstsegue", sender: self)


I neglected to give the segue itself an identifier name which you do by selecting the segue symbol and then selecting the Attributes Inspector and then giving the Identifier Field a name. thanks to all for their willingness to help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.