Crossed Ads

Tuesday 13 June 2017

How to implement Progress View in Objective-C with Xcode 8 ?

Progress View in iOS (Objective-C)


In this tutorial, we will see how to implement Progress View in iOS application using Objective-C Language.

Progress View is an object which is used to show progress of a particular task over a time. To do so, UIProgressView class is used into your iOS project. In our project, we used progress view object to progress on click event of button and progress value will be displayed in Label.

Step 1: Open Xcode and create new project by clicking on Single View Application template under iOS tab as shown in below image and click Next button. 



Step 2: Enter the product name as ProgressViewDemo, select the Language as Objective-C and again click Next button as shown in below image. Later click Create button to create project appear on the next window.




Step 3: Adding UIProgressView, UILabel & UIButton onto the View Controller.
On your project, click on main.storyboard file available in the project navigator, and then select the UIPickerView from object library, drag into the superview of View Controller, it will look like below.


  
Moreover, Progress View object is having its progress default value is 0.5 so make it zero to appear it as initially started.

In the same manner, Label and Button will also be placed into View Controller.


Step 4: Declaring property for Progress View, Label & Button.

Select the Assistant Editor and create IBOutlet property for UIProgressView object by holding Ctrl button. It will open up a popup view like below then enter the Name as ‘progressView’ and click Connect. 



In the same manner, set up the IBOutlet property for Label object & name it as ‘lblText’ and then hit ‘Enter’ button. While set up the IBAction property for Button & name it as ‘btnStart’ and then hit enter button.

Furthermore, we need to define property for NSTimer class through which progress view will get executed over a time.

@property (nonatomic) NSTimer *myTimer;

The all property defined in ViewController.h file will be shown as below: 



Step 5: Complete Code in ViewController.m file
In ViewController.m file, all code will be written in button action method where we schedule time using block which will be triggered after specified interval of time repeatedly if repeats value is set to YES



In block, we have taken a variable of type int named count which should be static type which is incremented by 1 on every call. This variable is used to iterate the progress of progress view object and static keyword is used to remember its value across invocation means its value never gets reset.

(The progress property has a value from 0.0 to 1.0. We are updating the value with 0.1 at each call).

When value of count variable reaches to 100, progress bar will be filled up  using if condition and will also show its value in label as well. In else part, timer variable will be invalidated and value initialised to nil.
That’s the complete code for UIProgressView.


Step 6: Finally, Build and Run the application where you will get the output like below. 


Have Fun !


You can also watch its video on below link:

1 comment: