Clear previous data from array - swift4

Here I created array
var viewArray : [UIImageView] = [UIImageView]()
func initialization() {
let position = self.multiDArray["positions"] as! [[String:Any]]
for i in 0...position.count {
let userImageView: UIImageView = UIImageView()
//code
viewArray[i].append(userImageView)
self.canvasView.addSubview(userImageView)
}
}
Here I draw images multiple time but when I want to get next images, previous images not getting removed.

To remove all of the views from your current view you can use:
for view in viewArray {
view.removeFromSuperView()
}
To empty your array you can remove all of the items using:
viewArrary.removeAll()

Related

Find a view at specific location in swift

I have got a simple view in swift and put 9 buttons as a 3x3 grid onto it, now i need the tag for each button and don't know how to get each button so I can get the tag property using a for loop. Does anyone know how i can get the button? Is there a function to get a view at a specified location?
Frankenstein's answer with .subviews works fine, but you can even do it more swifter using the built-in function viewWithTag(_:):
override func viewDidLoad() {
super.viewDidLoad()
// ...
// targeted view must have been added to the subview by now
if let taggedView = view.viewWithTag(1) {
print("Got the view!")
}
// you can even try to cast directly
if let taggedButton = view.viewWithTag(1) as? UIButton {
print("Got the button!")
}
// if you insist on using a for loop, you could use it like this
let highestTag = 10
for i in 0...highestTag {
if let taggedButton = view.viewWithTag(i) as? UIButton {
// here you go
}
}
}
For both approaches keep in mind, that the view you're looking for has already been added as a subview beforehand, otherwise you won't be able to search for it
You can use the subviews property of UIView and loop through each subview to check for tag you need:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
for subview in view.subviews {
if subview.tag == 1, let button = subview as? UIButton {
print("Got the button I need.")
}
}
if let button = view.viewWithTag(1) as? UIButton {
print("Got the button I need.")
}
}
}
Update: You can also use viewWithTag to find you view with a particular tag from within your subviews.

How to grab other local tag data in Swift?

I am working on saving data process,
because my data is small, so I want to save in local data storage.
I want a make time schedule.
Because I have single 35 buttons and label(each buttons and label are paired), So I am using UIButton tag now. And I have a problem on using other local data "tag".
I tired to make new func on global swift file(same swift file). but it is not able to call tag, because tag is only can use when UIbutton pressed.
I want to use tag property on viewDidLoad(), not only in UIbuttonPressed.
override func viewDidLoad() {
if let items = UserDefaults.standard.array(forKey: "SubjectList") as? [String] {
subjectArray = items
UITextLabel**1**.text = subjectArray**[1-1]**
}
}
#IBAction func buttonPressed(_ sender: UIButton) {
var textField = UITextField()
let newItem = textField.text!
if sender.tag == 1{
self.subjectArray[sender.tag-1] = newItem
print("1")
}
UserDefaults.standard.set(self.subjectArray, forKey: "SubjectList")
}
Create outlet collections for all the paired labels and buttons
#IBOutlet weak var allLbls:[UILabel]!
#IBOutlet weak var allBts:[UIButton]!
In viewDidLoad
if let items = UserDefaults.standard.array(forKey: "SubjectList") as? [String] {
subjectArray = items
subjectArray.indices?.forEach {
allLbls[$0].text = subjectArray[$0]
allBts[$0].tag = $0
}
}

Cannot call a function within a didSet. Error message: "expression resolves to unused function"

class HomeViewController: UIViewController
{
// The timer is used to check when the download is complete and will only allow segues when it is
var timer = Timer()
var segueName: String = ""
static var didSelectTabBar = false
static var tabBarSegueName = ""
static var startAnimation: Bool = false
{
didSet
{
if startAnimation == true
{
updateCounting(<#T##HomeViewController#>)
loadAnimation(<#T##HomeViewController#>)
}
}
}
The variable for the didSet is being changed in a separate Swift file and works, I can get to the if statement. But I can't get the functions to work and receive the same error message for both.
updateCounting
func updateCounting(){
if MyVariables.allSoldiersDownloaded
{
HomeViewController.startAnimation = false
loadingImageView.stopAnimating()
loadingImageView.isHidden = true
if HomeViewController.didSelectTabBar == false
{
self.performSegue(withIdentifier: self.segueName, sender: nil)
}
timer.invalidate()
MyVariables().setGlobalSoldier(id: MyVariables.facebookSoldierID)
soldierOfHourButton.setTitle("Soldier of the Hour: \(MyVariables.globalSoldier.christian_names) \(MyVariables.globalSoldier.surname)", for: UIControlState.normal)
soldierOfHourButton.sizeToFit()
}
else {
//print("Not downloaded yet")
}
}
loadAnimation
func loadAnimation() {
//creates and stores all the names for the images used in an array
var imagesNames = ["run1-1.jpg", "run2-1.jpg", "run3-1.jpg", "run4-1.jpg", "run5-1.jpg", "run6-1.jpg", "run7-1.jpg", "run8-1.jpg", "run9-1.jpg", "run10-1.jpg", "run11-1.jpg"]
//create new uiimage array
var images = [UIImage]()
//loop through all the photos in the imagesNames array and add them to the images array
for i in 0..<imagesNames.count{
images.append(UIImage(named: imagesNames[i])!)
}
//tell testview what images to use for the animation
loadingImageView.animationImages = images
//tell testview how long to show a single image for
loadingImageView.animationDuration = 0.9
//start the animation in the image view called test view
loadingImageView.startAnimating()
loadingImageView.isHidden = false
}
Welcome to SO. Please provide as much info as you can (for example the full stack trace), and format your questions properly (see my edits). This way you will get help much much faster.
What is the meaning of <#T##HomeViewController#>?
It should be just updateCounting() and loadAnimation():
Replace
updateCounting(<#T##HomeViewController#>)
loadAnimation(<#T##HomeViewController#>)
with
updateCounting()
loadAnimation()

How do I display the data fetched from called view controller into a dynamic tableviewcell of the calling view controller while using unwind segue.?

I have dynamic tableview, wherein one of the cell (duration) when tapped opens another view controller which is a list of duration viz (30 min, 1 hour, 2 hours and so fort). One of the durations when selected should display the selected duration in the first view controller. I am able to pass the data back to first view controller using unwind segue but unable to display the passed value. DOn't know whats missing.
I am displaying the code below:
FIRST VIEW CONTROLLER (CALLING)
#IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
var cell = tableView.dequeueReusableCellWithIdentifier("durationCell") as! durationTableViewCell
if let durationTableViewController = segue.sourceViewController as? DurationTableViewController,
selectedDuration = durationTableViewController.selectedDuration {
cell.meetingDurationCell.text = selectedDuration
duration = selectedDuration
}
SECOND VIEW CONTROLLER (CALLED)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SaveSelectedDuration" {
if let cell = sender as? UITableViewCell {
let indexPath = tableView.indexPathForCell(cell)
if let index = indexPath?.row {
selectedDuration = durationList[index]
}
}
}
}
tableView.dequeueReusableCellWithIdentifier should only be called within tableView:cellForRowAtIndexPath:. It has no use outside this context.
The easiest fix is to just reload the table once you have stored the selected duration:
#IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
if let durationTableViewController = segue.sourceViewController as? DurationTableViewController {
selectedDuration = durationTableViewController.selectedDuration
tableView.reloadData()
}
}
Note that this assumes you only need one selectedDuration for your whole table, rather than one per row. If you need one per row, I assume you have them stored in an array somewhere, so it is that array that should be updated instead before the reloadData.

Update Core Data Object Order - Not Working

Current Behavior
My core data keeps track of the values for a UITableView list. Each row has a title and description. My core data is working for appending new records and later deleting them. Core data is also working great for editing the content of the existing records.
Problem/Question
I just added drag and drop functionality in the table. On the surface, it works perfectly. I can drag the top item to the bottom, middle to the top, etc. However, the new list order does not persist after app shutdown because the Core Data records aren't being updated.
I found a few tutorials on this but none seem to work with my code. I also spent a few hours trying to use and adapt my current Core Data skill set (update, delete, edit) to invent a solution. I have no sweet moves or code kung fu.
Should you choose to accept this mission, below are the details and code you might need.
Information
Coding in Swift
Using X-Code 6.4
Core Data Info:
File name is: CD_Model
Entity name: TodayTask
Attribute names: "name" and "desc"
Code:
Main list variable:
var todayTaskList = [NSManagedObject]()
ViewDidLoad for main list page with UITableView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Break
//Load the list from Core Data
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"TodayTask")
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject]
if let results = fetchedResults {
todayTaskList = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
//Break
//This provides a variable height for each row
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
//Break
//Part of code for cell drag and drop functionality
let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")
tableView.addGestureRecognizer(longpress)
}
Table Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Table View & Cell Setup
//***** ----- ***** ------ ***** ----- ***** ----- *****
#IBOutlet weak var name_Label: UILabel!
#IBOutlet weak var desc_Label: UILabel!
//Tells the table how many rows it should render
//*Looks to the Core Data NSObject to count tasks
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todayTaskList.count
}
//Creates the individual cells. If the above function returns 3, this runs 3 times
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Setup variables
let cellIdentifier = "BasicCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell
let task = todayTaskList[indexPath.row]
//Create table cell with values from Core Data attribute lists
cell.nameLabel!.text = task.valueForKey("name") as? String
cell.descLabel!.text = task.valueForKey("desc") as? String
//Make sure the row heights adjust properly
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
return cell
}
And here is where my problem is, the drag and drop. This code works, but it is missing code that rearranges the core data. Without that code, any drag/drop re-ordering will reset when I close the gap:
//This function initiates the Drag & Drop code.
func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
let longPress = gestureRecognizer as! UILongPressGestureRecognizer
let state = longPress.state
var locationInView = longPress.locationInView(tableView)
var indexPath = tableView.indexPathForRowAtPoint(locationInView)
struct My {
static var cellSnapshot : UIView? = nil
}
struct Path {
static var initialIndexPath : NSIndexPath? = nil
}
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;
var dragCellName = currentCell.nameLabel!.text
var dragCellDesc = currentCell.descLabel.text
//Steps to take a cell snapshot. Function to be called in switch statement
func snapshopOfCell(inputView: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
inputView.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
let cellSnapshot : UIView = UIImageView(image: image)
cellSnapshot.layer.masksToBounds = false
cellSnapshot.layer.cornerRadius = 0.0
cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
cellSnapshot.layer.shadowRadius = 5.0
cellSnapshot.layer.shadowOpacity = 0.4
return cellSnapshot
}
switch state {
case UIGestureRecognizerState.Began:
//Calls above function to take snapshot of held cell, animate pop out
//Run when a long-press gesture begins on a cell
if indexPath != nil {
Path.initialIndexPath = indexPath
let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
My.cellSnapshot = snapshopOfCell(cell)
var center = cell.center
My.cellSnapshot!.center = center
My.cellSnapshot!.alpha = 0.0
tableView.addSubview(My.cellSnapshot!)
UIView.animateWithDuration(0.25, animations: { () -> Void in
center.y = locationInView.y
My.cellSnapshot!.center = center
My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)
My.cellSnapshot!.alpha = 0.98
cell.alpha = 0.0
}, completion: { (finished) -> Void in
if finished {
cell.hidden = true
}
})
}
case UIGestureRecognizerState.Changed:
//Runs when the user "lets go" of the cell
//Sets CG Y-Coordinate of snapshot cell to center of current location in table (snaps into place)
//If the indexPath is not 0 AND is not the same as it began (didn't move)...
//Update array and table row order
var center = My.cellSnapshot!.center
center.y = locationInView.y
My.cellSnapshot!.center = center
if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
swap(&todayTaskList[indexPath!.row], &todayTaskList[Path.initialIndexPath!.row])
tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
Path.initialIndexPath = indexPath
}
default:
//Runs continuously as there's a long press recognized?
//Animates cell movement
//Completion block:
//Removes snapshot of cell, cleans everything up
let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
cell.hidden = false
cell.alpha = 0.0
UIView.animateWithDuration(0.25, animations: { () -> Void in
My.cellSnapshot!.center = cell.center
My.cellSnapshot!.transform = CGAffineTransformIdentity
My.cellSnapshot!.alpha = 0.0
cell.alpha = 1.0
}, completion: { (finished) -> Void in
if finished {
Path.initialIndexPath = nil
My.cellSnapshot!.removeFromSuperview()
My.cellSnapshot = nil
}
})
}
I am pretty sure the code I need would go inside the second case statement:
case UIGestureRecognizerState.Changed:
I also think the code I need would start with something like...
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var context: NSManagedObjectContext = appDel.managedObjectContext!
But then is there a special code for rearranging? Do I have to delete and insert? If so, how?
HUGE thanks in advance to anyone who can help resolve this one!
Well first of all, you might find it easier to create classes for each entity so that you don't have to work with objects vaguely typed to NSManagedObject or read and cast with valueForKey(_:). In the solution below, I've included code samples for that.
So to solve your order problem, there are two things you could do:
1) Add a property that defines the order of your Task entity. This can be a simple as an NSNumber called displayOrder. Your fetch request can then order the results according to that property. Then, when your table cells are re-arranged, iterate through the task list and update the displayOrder property of each task to reflect the order in which they are being displayed. Save your managed object context and the next time your fetch request loads, it will order them accordingly.
class Task: NSManagedObject {
#NSManaged var name: NSString
#NSManaged var desc: NSString
#NSManaged var displayOrder: NSNumber
}
let fetchRequest = NSFetchRequest()
let sortDescriptor = NSSortDescriptor(key: "displayOrder", ascending: true )
fetchRequest.sortDescriptors = [ sortDescriptor ]
2) Create a CoreData entity that represents a list with a to-many relationship that stores each task entity in an ordered set. Then, when you add tasks to the set, they will be remain saved in the order you've added them.
class TaskList: NSManagedObject {
#NSManaged var tasks: NSOrderedSet?
}
class Task: NSManagedObject {
#NSManaged var name: NSString
#NSManaged var desc: NSString
#NSManaged var parentList: TaskList?
}
Update to answer remaining questions:
I highly recommend you use your own custom classes instead of NSManagedObject, but until you figure that part out here's what you can do to your code as is.
To update display order after rearranging or deleting:
func updateDisplayOrder() {
for i in 0..<todayTaskList.count {
let task = todayTaskList[i]
task.setValue( i, forKey: "displayOrder" )
}
}
To append a new task:
func addTask( task: NSManagedObject, displayOrder: Int ) {
todayTaskList.insert( task, atIndex: displayOrder )
updateDisplayOrder()
}