Hi there guys I have movies array in MoviesViewController which has a tableView and where I display tableCells. And I have logout in SidebarMenuController which onTap goes to LogInController. When I hit logout I want to empty the movies array from MoviesViewController and go to LogInController. How to go about this?
I would try creating an Event class that holds the array of movies. Then add an optional event property to MoviesViewController and observe setting it. On sign out, make the array empty, and set the event for the view controller. So the view controller would have something like:
var event: Event? {
didSet {
moviesArray = event?.movies
tableView.reloadData()
if moviesArray.isEmpty {
presentLogin()
}
}
}
This could also be used to populate the array if getting from a service.
Related
I have Tab Bar Controller, where I have few view controllers, but I want to pass array of values (workoutNames) to another view in my Tab Bar Controller. I wonder what's best option to do this and I've decided to use way of passing data with property. But when I try to retrieve data I get empty array. I could also use firestore to retrieve data in my Second View Controller, but It lasts too long, so I decided to passing data between views than retrieve data from firestore every time.
First View Controller
class HomeTableViewController: UIViewController
// I need to pass this array to another view
var workoutsName: [String] = []
...
func sendDataToCalendar() {
// IN THIS FUNCTION I RETRIEVE DATA FROM FIRESTORE AND UPDATE ARRAY WITH VALUES
// After all I print this array to check if everything is correct, and my data is here
print("\(workoutsName)")
}
Here is my Second View Controller when I want to use array from First View Controller
class CalendarViewController: UIViewController {
var arrayOfTitles = [String]()
.
.
.
func getArrayFromHome() {
let homeVC = HomeTableViewController()
homeVC.workoutsName = arrayOfTitles
// NOW I PRINT TO CHECK IF DATA EXISTS
print("\(arrayofTitles)"
}
And its empty, so data didn't pass.
Maybe it's not the best way to pass data, but main idea of this is that I will need to use this array in few view controllers. These controllers won't be connected by segue, so I can't use prepareforSegue. It's not one to one relationship (in future I will need this array in few controllers), so I shouldn't use delegate. I don't think that notification will be ok too, so I think that it's best option to pass data by property. But maybe I am wrong, so please correct me.
The reason why it doesn't work is that you instantiate a new HomeTableViewController with empty data.
If this data will be used on lots of place, why not save it locally? user default seems like it fit your needs.
func sendDataToCalendar() {
UserDefaults.standard.set(workoutsName, forKey: "workoutsName")
}
Then you can read it later on
func getWorkoutNameArray() {
let workoutName = UserDefaults.standard.object(forKey: "workoutsName") as? [String]
}
In your getArrayFromHome() function you are not accessing HomeTableViewController instance where you got the data but creating a new instance. That's why the array is empty. You end up with 2 instances of HomeTableViewController, one in use with the correct array and the dummy one created in the function with an empty array.
Would be better if you pass the data in the same place where you have a reference to CalendarViewController.
Let's say that you are creating and presenting CalendarViewController in your HomeTableViewController like:
let calendarViewController = CalendarViewController()
calendarViewController.arrayOfTitles = workoutNames
// Here is the presentation or push of calendarViewController
It will be useful for you to read this SO question
1) I am working on one application which contains one single tableview and above that tableview there is one horizontal scrollview which contains number of dynamic button eg.button1,button2....
2)On touch of each button json is comming and i am parsing and showing that json in tableview using NSURLSession.eg.One touch of button1 json will come,on touch of button2 different json will come.
3)After that i want to store each button json array into single array and display accordingly.eg. suppose i clicked on button2 json will come and should be stored in array so that in future i retouch that button data should come from stored array.
I've managed point 1 & 2 but not getting any idea how to solve 3 point.
I also added the one image which will give clear understanding about my problem.
Please if any one have idea then reply to this question.Thanks in advance.
You can use NSCache to cache items if you want, it works like an NSDictionary.
// Init NSCache and declare somewhere as property//you must use singleton approach as well, it's just example i have initialized in viewDidLoad
- (void)viewDidLoad
{
self.cache = [NSCache alloc] new];
}
-(void)callApiForKey:(NSString *)buttonName{
id aObject = [self.cache objectForKey:buttonName]
if aObject == nil {
//perform the API call you are talking about
//after the fetch is preformed and you get data...
[self.cache setObject: dataObject forKey:buttonName];
//perform what you need to with said object
} else {
//perform operation with cached object
}
}
USES:
on your button click action call above method with buton text:
yourButton.titleLabel.text.
[self callApiForKey:yourButton.titleLabel.text];
be suer keys must be unique. other wise you may get/set wrong data obviously.
Thanks
-Each button have a unique tag right? Like button 1 have tag 1, button 2 have tag 2 ,button 3 have tag 3,
-you can manage this thing by Global nsmutabledictionary and nsmutableArray.
-if you click on button 1 before api calling you check
if(dictionary valueforkey:#"1") (1mean selected button tag)
{
no need to call api
no need to add array in dictionary
array=[[dictionary valueforkey:#"1"]mutablecopy];(1mean selected button tag)
[tble reloaddata];
}
else
{
api calling
here you can add array in dictionary
[dictionary setobject:yourarray forkey:#"1"];
array=[[dictionary valueforkey:#"1"]mutablecopy];(1mean selected button tag)
[tble reloaddata];
}
I know this is a startlingly stupid question, but I can't figure it out. Every answer involves a UISearchBar, which is not what I've got.
I'm trying to display 2 sets of results on one TableViewController.
Results to display
1) everything in my managedObjectContext which is set up in my viewDidLoad
2) a filtered set of results if a predicate is selected.
On MyTableViewController, I have a popover which instantiates when I click a UIBarButtonItem on MyTableViewController. On the popover, I set a predicate on MyTableViewController.
Basically, I'd like to toggle what's displayed and that display toggle is driven by whether my variable is nil (everything displays) or filtered (variable sets a predicate).
Have two NSArray properties allValues and filteredValues. Set up all your delegate/dataSource properties using your filteredValues array.
Next, do something like this when you first get all your data:
self.allValues = [someController fetchAllValues];
self.filteredValues = self.allValues;
[self.myView.tableView reloadData];
Last, alter your filteredValues array whether or not a predicate is selected:
if (self.selectedPredicate) {
self.filteredValues = [self.allValues filteredArrayUsingPredicate:self.selectedPredicate];
} else {
self.filteredValues = self.allValues;
}
[self.myView.tableView reloadData];
When I go to a viewController I call within my viewDidAppear Method a function:
override func viewDidAppear(animated: Bool) {
getLessons()
}
This methods loads from parse.com a list of data I want to use in a pickerView.
The function itself:
func getLessons(){
var query = PFQuery(className:"Lesson")
query.orderByAscending("name")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
var name = object["name"] as String
self.languagePickerKeys.append(object.objectId)
self.languagePickerValues.append(name)
self.selectedLanguage.text = self.languagePickerValues.first // set the first lessons name into the text field
self.selectedLessonObjectId = self.languagePickerKeys.first // set the first objectId for the lesson
self.languagePicker?.reloadAllComponents()
}
} else {
// Log details of the failure
println("\(error.userInfo)")
}
}
println("getLessons done")
}
The thing is, that the textfield is empty, as the getLesson() gets the data async and the data is not available to the textfield.
I also tried to put the getLesson into the viewDidAppear method, but this doesn't help me, the textfield is empty anyway.
What can I do, to have the data from the getLessons() method ready and loaded its first value into my textfield when the view is shown to the user?
You certainly have to get the data from asyncTask before setting it to pickerView.
Here's the ViewController lifecycle after instantiation:
Preparation if being segued to.
Outlet setting
Appearing and Disappearing.
So, you have two options:
Load the data in previous ViewController and then perform the segue. You need to follow these steps for it.
a. Create a segue from previous ViewController to your ViewController.
b. Call the function when you want to go next ViewController which fetches the data, and the end (after getting the data) call performSegueWithIdentifier which will lead to your ViewController.
c. Set the data in prepareForSegue
let navigationController = segue.destinationViewController as UINavigationController
navigationController.data = yourData //you got from async call
Now when you reach your ViewController, you are sure that your data is present, and you can set it to your pickerView.
If you want to do it in the same ViewController: here's is the lifeCycle of ViewController:so you need to call your function in viewDidLoad, and always set your pickerView after completion of the async network call.
Make sure that you initiate all changes to the UI from the main thread e.g. like so:
dispatch_async(dispatch_get_main_queue(), {
selectedLanguage.text = languagePickerValues.first
self.languagePicker?.reloadAllComponents()
})
The problem is that findObjectsInBackgroundWithBlock is an asynchronous method, so even if you fire it in the ViewDidLoad you will never know when you will receive the response data and you can't be sure that the data will be ready by the time you view appear.
I think you have just 2 possibility:
The first one is to load the data in the previous view controller and then just pass the data that got ready to you view controller.
The second is to use a synchronous method (the findobject method maybe?) and put the call in a method that is fired BEFORE the view appear (like the viewWillAppear: method). But your view will stuck for a moment (I think) while the data is retreiving... However this second solution probably resolve your problem but using synchronous method to retrieve data from a slower data source is usually bad design solution.
D.
I´m using a table view to display different types of items. The view controller for the table is always the same, ItemsViewController.
I have a menu with buttons for each type of item. Depending on what button the user clicks, a specific array of items must be used to populate the table view.
My solution:
Every item has an typeId property. When a type button is clicked, the following method is called:
[self.itemController createItemListForId:buttonPressed.typeId];
where buttonPressed is, well, the pressed menu button.
This is the implementation for createItemListForId:
-(void)createItemListForId:(int)theId{
for (Item *item in self.masterItemList){
if(item.typeId == theId){
[self.itemList addObject:item];
}
}
}
The masterItemList is popluted with all the items at initialization.
My ItemsViewController uses the itemList to populate the cells.
The problem is that itemList is empty when the table view appears, so something not working with this method.
Any ideas on how to get this to work?