使选择器的选定值成为第一个选择。

huangapple go评论51阅读模式
英文:

How to make Picker's selected value the first choice

问题

I'd like to set selectedMenu to "Select Item" (The first item of picker selection).
WIth NSPopUpButton, there is selectItem(at: index), but I can't find out a way to do like so in SwiftUI Picker...
How to I do it?

我想将selectedMenu设置为"Select Item"(选择器选择的第一个项目)。在NSPopUpButton中,有selectItem(at: index)的方法,但我找不到在SwiftUI Picker中实现类似功能的方法。应该如何做呢?

英文:

I'm trying to my macOS app which UI was written in StoryBoard into SwiftUI. I am beginner of SwiftUI...

I have swiftUI Picker like the following:

import SwiftUI

struct MessageAreaView: View {

// opponent name
@ObservedObject var anOpponentName: ShareValues

// menu type picker selection
@State var selectedMenu: String = ""

// menu type picker content
let defaultMenus = ["Hi. How about a game?", "Good luck!", "I give in.", "Thank you for the game.", "Let’s play again sometime.", "My hand slipped. May I undo?", "Hello.", "Goodby.", "Thanks."]

var body: some View {

	// Menu type picker
	Picker(selection: $selectedMenu, label: Text(""), content: {
	    Text("Select Item").tag("") // To avoid "picker: the selection "" is invalid..."
	    ForEach(defaultMenus, id:\.self) { aMenu in
	       Text(aMenu)
	    }
	})
	.pickerStyle(.menu)
	.onChange(of: selectedMenu, perform: { selected in
	    	        
        if !(selected == NSLocalizedString("Select Item", comment: "Select Item")) {
            
            let anOpponent = anOpponentName.anOpponentName
            
            let commentToSend = NSString(format: "TELL %@ %@\r\n", anOpponent, selected) as String
            mySocket.sendMessage(message: commentToSend)
        }
        // I'd like to set selectedMenu to "Select Item" here...
	})
	.frame(width:470)
}

I'd like to set selectedMenu to "Select Item" (The first item of picker selection).
WIth NSPopUpButton, there is selectItem(at: index), but I can't find out a way to do like so in SwiftUI Picker...
How to I do it?

答案1

得分: 1

首先,对比 selected == NSLocalizedString("Select Item" 将始终为 false,因为 "Select Item" 是菜单项的显示标题,而不是空字符串的值/标签。

其次,Swift 提供了一种更方便的格式化字符串的方式,称为字符串插值。

要将选择器重置为 "Select Item",请将属性 selectedMenu 设置为空字符串。

.onChange(of: selectedMenu) { selected in
    if !selected.isEmpty {
        let anOpponent = anOpponentName.anOpponentName
        let commentToSend = "TELL \(anOpponent) \(selected)\r\n"
        mySocket.sendMessage(message: commentToSend)
        selectedMenu = ""
    }
}
英文:

First of all the comparison selected == NSLocalizedString("Select Item" will always be false because "Select Item" is the displayed title of the menu item but not the value/tag which is an empty string.

Second of all Swift provides a more convenient way to format strings called String Interpolation.

To reset the picker to "Select Item" set the property selectedMenu to an empty string.

.onChange(of: selectedMenu) { selected in
                
    if !selected.isEmpty {
        let anOpponent = anOpponentName.anOpponentName
        let commentToSend = "TELL \(anOpponent) \(selected)\r\n"
        mySocket.sendMessage(message: commentToSend)
        selectedMenu = ""
    }
}

huangapple
  • 本文由 发表于 2023年4月17日 15:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定