新手求助:成功上传图文后如何切换至启动窗口与TableView?
实现上传成功后切换至TableView的方案
Hey there! As a new developer, let's walk through how to switch back to your TableView after successfully uploading your image and text. Here's how to modify your existing code and handle the navigation:
Step 1: Complete the Upload Callback
Your current code starts setting up the upload, but we need to add the completion block for Parse's save operation to trigger the navigation once the upload succeeds. Here's the updated code for your postImage function:
@IBAction func postImage(_ sender: Any) { guard let image = imageTopost.image, let currentUser = PFUser.current() else { // Handle cases where image is nil or user isn't logged in print("Missing image or logged-in user") return } let post = PFObject(className: "Post") post["message"] = comment.text post["userId"] = currentUser.objectId // Prepare image data guard let imageData = image.pngData() else { print("Failed to convert image to data") return } // Setup activity indicator let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) activityIndicator.center = self.view.center activityIndicator.style = .large activityIndicator.hidesWhenStopped = true self.view.addSubview(activityIndicator) activityIndicator.startAnimating() // Save the post to Parse with completion block post.saveInBackground { [weak self] (success, error) in guard let self = self else { return } activityIndicator.stopAnimating() activityIndicator.removeFromSuperview() if success { // Upload succeeded! Now switch back to TableView self.navigateToTableView() } else { // Handle upload failure print("Upload failed: \(error?.localizedDescription ?? "Unknown error")") // Optionally show an alert to the user let alert = UIAlertController(title: "Upload Failed", message: error?.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) self.present(alert, animated: true) } } }
Step 2: Add the Navigation Logic
Create a helper function navigateToTableView() to handle switching back to your TableView. Choose the option that matches your app's screen structure:
private func navigateToTableView() { // Option 1: If you pushed this upload screen via a Navigation Controller navigationController?.popViewController(animated: true) // Option 2: If you presented this upload screen modally // dismiss(animated: true, completion: nil) // Option 3: If using a Tab Bar Controller (switch to the tab with TableView) // if let tabBarController = tabBarController { // tabBarController.selectedIndex = 0 // Change to your TableView's tab index // } // Option 4: If you want to go back to the root view controller (where TableView is) // navigationController?.popToRootViewController(animated: true) }
Which Option to Choose?
- Option 1: Use this if you used
navigationController?.pushViewController(...)to show the upload screen. - Option 2: Use this if you used
present(...)to show the upload screen as a modal. - Option 3: Use this if your app uses a TabBar, and your TableView is one of the tab items.
- Option 4: Use this if your TableView is the root screen of the navigation stack.
Key Notes for New Developers
- Always use
[weak self]in completion blocks to avoid memory leaks (you'll see this in the updated code above). - Add error handling to let users know if something goes wrong (like the alert in the failure case).
- Test each navigation option to make sure it works with your app's specific screen flow.
内容的提问来源于stack exchange,提问作者Daniel.P
相关产品推荐
相关产品推荐

