Subscription app - ios

I am currently developing a subscription app.
I currently have 6 View Controllers to which have different options on.
For example - Name - Amount - Date etc.
Once the user completes these, I want it to create a new view controller with this information on it.
So once they go back onto the main screen, they will be able to swipe left and it will show their subscription.
All I want to know is, is there a way I can create a new view controller with their information on it once they press a complete button on the Date page?
Transferring data to a view controller is the easy part. I am more wondering on how I can create a new view controller when they try and add another Subscription, if that makes sense.
If not is there a way I can do this?

You have two options that I can think of. I am sure there are more, but these may help.
You could create a global instance of a struct that is your "subscription to add". You could then update the values for each property of this struct (e.g. name, amount, date, etc.) after each VC is dismissed
You can pass the data your user is generating to each VC through prepareForSegue. This is slightly redundant so I would recommend doing the first method. For the record, this segue method would look something like this:
VC 1 Dismissed: Pass Name to VC2
VC 2 Dismissed: Pass Name and Amount to VC3
VC 3 Dismissed: Pass Name, Amount, and Date to VC4 etc......
Once you have all of the information, you can show the last VC and just set the labels and such to the values of your struct, or the values you passed through the segue.
Edit: Further Information on Option 1
So if you made a struct like this:
struct UserToAdd {
static var name: String = String()
static var amount: Int = 0
static var date: Date = Date()
}
with all of the properties that you want a subscription to have, then you can store this information in this struct as you progress through each VC.
For example, if your first VC got the subscription name, then when your "prepareForSegue" function is called, as you are about to move to the second VC, then you could do something like this:
UserToAdd.name = "My Subscription Name"
Of course the string you assign to this name would depend on the data the user enters and such, but I hope it illustrates my point. It would be a similar process for each initial VC, but then once you have all of the data you need, you could just then call this data when your last VC loads, and set it to the text of a label or something like this:
var nameLabel = UILabel()
nameLabel.text = UserToAdd.name
self.view.addSubview(nameLabel);
Let me know if you have any further questions.

Related

Swift: How to pass Data into my shopping Cart(Array)?

Im new here and trying to write my first little App in Swift.
I have to program a little Book-Shopping-App but now I have a problem.
If I press the "add to card"-Button in my DetailView I want to pass my Book-Data to my Shopping-Card-Class. The Shopping-Card-Class is a TableView with an Array.
So this my DetailView: http://fs5.directupload.net/images/160120/tpsdezys.png
If I press this I want to pass it to my Shopping-Card-Class/Screen, here:
http://fs5.directupload.net/images/160120/qhsbi6aq.png
In my DetailView is all the book stuff like :
var autorName: String?
var title: String?
var price: String?
var image: String?
And in the Shopping-Card-Class is a Array, thats it, but I dont know how to pass the data :/
If I press the button I just add the book, I dont switch the screens.
I've used Delegates before but never in combination with arrays,
pls help :/
I used it here, look: fs5.directupload.net/images/160121/8zmfqef4.png
and passed the data to this screen:
fs5.directupload.net/images/160121/2263tiwl.png
I would suggest starting here!
Once you go through these solutions, you will have a much better understanding on how to pass information between ViewControllers. As a hunt you will need to mess around with the prepareForSegue method and use a segue identifier in order to see which view controller you are jumping to, then setting an instance variable of that class equal to the data you have available in this class. This essentially passes the data from one class to the other.

How to Select an Instance Variable in a Separate UIViewController Without Using a Segue

I'm trying to select and set an instance variable that is part of another UIViewController, however, I don't know how to select another ViewController and access its contents without using a segue.
Whenever a user checks off a task, a percentage of tasks that are complete should be calculated and another view controller's instance variable should be set.
I realize I'm currently instantiating a new view controller instead of selecting the one I already have on the storyboard. I'm using a third party sidebar menu that resides behind my main view, although it really exists as a separate Scene/ViewController. It should be noted that this sidebar menu doesn't use a segue reveal itself. Is there any method to select another view controller and access it's instance variables?
#IBAction func checkOffTask(sender: UIButton) {
// Select sidebar view controller
let sidebarViewController = self.storyboard?.instantiateViewControllerWithIdentifier("sideBarScene") as! SideBarViewController
// Calculate percentage of completed tasks
// Select the count of all tasks
let allTasksCount = Float(firstDataSource.count + secondDataSource.count)
// Select the count of all completed tasks
let completedTasksCount = Float(secondDataSource.count)
// Divide the two to get a percentage
var completedTaskPercentage = completedTasksCount / allTasksCount
sidebarViewController.completedTaskPercentageTemporary = String(stringInterpolationSegment: completedTaskPercentage)
println(sidebarViewController)
println(sidebarViewController.completedTaskPercentageTemporary)
}
One trick could be find out current visible view controller and set properties on it. This thread may help you doing this.
Another way could be to get hold to targeted view controller's object when it is intialized by storyboard. For this you would need to rely on prepareForSegue:sender: method. Once you have saved the object you can pass it & use it to set the properties.

Access label text in Swift

I have created a timer class in swift and when a user clicks a button I segue to another view and pass data between the two views. The time class uses 3 separate labels for hour, minute and second however I would like to pass all 3 in a single variable.
My question is, how do I access the text inside a label. If I use "\(hourLabel.text)" (for example) I get a message "Optional(00)".
If you're trying to access another view controller's view objects (a UILabel, for example) don't do that. It violates the principle of encapsulation, and also often doesn't work.
If try to evaluate hourLabel.text where hourLabel is an outlet in your current view controller, the outlet link is probably broken (and nil.)
Post the actual code you are trying to use.
Use this...
if hourLabel.text != "" {
println("\(hourLabel.text!)")
}
Why don't you try this...
if(!hourText.text){
// Do something...
}

Swift ios relational picker views and apple dev guidelines

Right now I have a picker view that shows up when you press a label, and after you have selected anything from the picker view and hit done it will hide and the label will change to the value you selected.
But I want to implement another picker view, and that picker view will only display based on the value you selected in the first picker view.
So more or less like a relational dropdown that you can find on almost every website.
I want to be able to select category and subcategory. But its only about 10% of the categories that has an subcategory thats why I want to build it this way.
So my question now is if this will be against apples dev/design guidelines?
Or does anybody else have a good solution on how to display a category/subcategory selector for a search form in an iOS app?
Thanks in advance,
Just a rough sketch:
Say you pick a category from you PickerView. Your PickerView should then notify you parent ViewController that the user has picked a Category. The most convenient way to do this is to have a Delegate method, like:
self.delegate.userPickedCategory(pickedCategory: Category)
Now, I assume you Category object contains an array of subcategories:
class Category: NSObject {
var title: NSString!
var subCategories: NSMutableArray!
//some variable containing categories content
}
Say you named the button to your Sub Category Menu subCategoryButton. You should always set hidden = true or at least userInteractionEnabled = false, because you don't know whether the picked category has a sub.
If your parent ViewController receives the delegate method that your user picked a category, you might do:
func userPickedCategory(pickedCategory: Category) {
if pickedCategory.subcategories != nil || pickedCategory.subCategories.count != 0 {
//you now know the picked category has a subCategory
//so allow the user to pick that subCategory by enabling this button
self.subCategoryButton.hidden = false
self.subCategoryButton.userInteractionEnabled = true
}
Then, you need to make sure the subCategoryButton shows another picker view containing the subCategories of your picked Category
Check the following open source implementation of what you want.
kxmenu

int value still remain the same when UIViewController dismissed iOS

I give an example:
I am using two view controllers, lets name it Home and Sales. Firstly, i navigate from Home To Sales using push. In Sales view controller i have globally declared an int value and assign it to zero. Like this:
int example_int = 0;
After that, there is some process that going in Sales View Controller, and example_int value changed, for example it has value of 2. Next, it navigate back to Home using pop. For testing purpose, i navigate back to Sales and i NSLog example_int, it gave the same value of 2.
This gave me problem initially. But i have solved it by using func: viewWillDismiss and assign the example_int to 0(default value). So for now, i m just curios what is going on here.Thank you.
The global variable will persist through out the app life cycle, if the use of example_int variable is limited to sales view than in that case you should create example_int as the member variable of sales view controller. If at all there is a requirement to keep the example_int variable as global than in that case you need to reset the value of example_int to 0 as per your requirement (in your case inside viewDidDisappear or viewDidUnLoad methods of sales view controller).

Resources