How do I get a popup dialog box in Swift Playgrounds
Alerts need to be presented from a view controller. than means its going to show up in the simulator inside the assistant editor:
Example:
import UIKit
import PlaygroundSupport
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
let v = UIViewController()
PlaygroundPage.current.liveView = v
v.present(alert, animated: true, completion: nil)
Swift Tutorial GuidedTour.playground does nothing when opened in XCode6
After many tries, I found out that when I download the file, it asks me if about the extension (in Chrome, sorry in French).
I always answered "Use .playground" and it resulted in an unusable file as listed by the question here. Finally I tried to use .zip extension, unarchive the file, and it works just as fine !
Hope it helps :)
How to create and get return Value from Cocoa Dialog?
You can call an NSAlert and put the NSTextField as it's accessoryView like this"
- (NSString *)input: (NSString *)prompt defaultValue: (NSString *)defaultValue {
NSAlert *alert = [NSAlert alertWithMessageText: prompt
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:defaultValue];
[input autorelease];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[input validateEditing];
return [input stringValue];
} else if (button == NSAlertAlternateReturn) {
return nil;
} else {
NSAssert1(NO, @"Invalid input dialog button %d", button);
return nil;
}
}
Popup alert user before delete TableViewCell in swift?
All you have to do is present the alert when the button is pressed and set each action.
Replace your commit editingStyle
delegate method with this and replace the data
variable with your data array:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
presentDeletionFailsafe(indexPath: indexPath)
}
}
func presentDeletionFailsafe(indexPath: IndexPath) {
let alert = UIAlertController(title: nil, message: "Are you sure you'd like to delete this cell", preferredStyle: .alert)
// yes action
let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
// replace data variable with your own data array
self.data.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
alert.addAction(yesAction)
// cancel action
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
EDIT
Example:
private let reuseId = "cellReuseId"
class SlideToDeleteViewController : UIViewController {
lazy var tableView = createTableView()
func createTableView() -> UITableView {
let tableView = UITableView()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseId)
tableView.dataSource = self
tableView.delegate = self
return tableView
}
var data = ["one", "two", "three", "four"]
override func loadView() {
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension SlideToDeleteViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseId)
cell?.textLabel?.text = data[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
presentDeletionFailsafe(indexPath: indexPath)
}
}
func presentDeletionFailsafe(indexPath: IndexPath) {
let alert = UIAlertController(title: nil, message: "Are you sure you'd like to delete this cell", preferredStyle: .alert)
// yes action
let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
// put code to remove tableView cell here
self.data.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
alert.addAction(yesAction)
// cancel action
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
string urlsession response is not handled correctly
welcome to SO and Swift!
Your server is sending "EXIST"
, but you are checking for EXIST
.
IOW, instead of string.elementsEqual("EXIST") == true
, try string.elementsEqual("\"EXIST\"") == true
.
But don't stop there. Simplify the expression to string == "\"EXIST\""
.
Related Topics
Swift Spritekit Get Visible Frame Size
How to Center Nspopover When Using Swiftui
Swift-Animate Cashapelayer Stroke Color
Swift, Detect Ibeacons on The Background and Send Notifications When in Range
Bleed Through from Nsbutton Checkbox on Non-Transparent Nspopover
Swift - Rotate Gesture and Rotation Increments of 90 Degrees
Communication Error When Uploading 90 Mb IPA File with Swift to Appstore
Loading Many UIimages from Disk Blocks Main Thread
Read UId from Nfc Mifare Tag iOS 13
How to Pass Data Backwards to a View Controller After Passing Forward
Nsdocumentcontroller.Opendocument Not Allowing Selection of Custom File Type
Swiftui Conditional View Transitions Are Not Working
How to Add Skspritenode in a Loop
Spritekit Skscene Not Resizing Correctly to Fit iPhone 12
What Is The Intended Use of Optional Variable/Constant in Swift