如何在SwiftUI中在没有NavigationButton的情况下导航到NavigationView中的另一个视图?

时间:2019-06-12 07:04:17

标签: ios swiftui

正如问题所述,我想导航到另一个没有NavigationButton的视图,例如UIkit中的“ pushView”和“ popView”

2 个答案:

答案 0 :(得分:2)

SwiftUI的PresentationButton用于以模态形式在现有视图控制器上呈现新的视图控制器,例如在 UIViewController

上调用 present()
  

注意 PresentationButton 已在 Xcode 11.0 beta版中重命名为 PresentationLink 3

PresentationLink

例如,如果您具有下一个这样的视图:

@Bean
public JavaMailSenderImpl mailSenderService(ClassImpl ci) {
....

然后您可以这样显示它:

struct NextView: View {
var body: some View {
    Text("NextView")
}
}

除非有导航按钮,否则演示文稿按钮不需要导航视图。

参考目的我从here那里得到了答案

答案 1 :(得分:1)

我认为这更像是一种限制的解决方法,但它对我有用:

添加一个布尔状态变量,该变量定义链接是否处于活动状态。例如:

class AllCategoryAdapter(val categoryList : List<AllCategoryBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
{
    private val TYPE_HEADER : Int = 0
    private val TYPE_LIST : Int = 1

    override fun getItemViewType(position: Int): Int {

        if(position == 0)
        {
            return TYPE_HEADER
        }
        return TYPE_LIST
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == TYPE_HEADER)
        {
            val header = LayoutInflater.from(parent.context).inflate(R.layout.cv_all_category_header,parent,false)
            return ViewHolderHeader(header)
        }

        val header = LayoutInflater.from(parent.context).inflate(R.layout.cv_all_category,parent,false)
        return ViewHolder(header)
    }

    override fun getItemCount(): Int {
        return categoryList.size + 1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val listItem : AllCategoryBean = categoryList[position]

        if(holder is ViewHolderHeader)
        {
            holder.tvCategoyName.setText("All Category")
        }

        if(holder is ViewHolder)
        {
            holder.tvCategoyName.setText(listItem.getCategoryName())
        }
    }

    class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
    {
        val tvCategoyName = itemView.findViewById(R.id.tvCategoyName) as TextView
    }

    class ViewHolderHeader(itemView : View) : RecyclerView.ViewHolder(itemView)
    {
        val tvCategoyName = itemView.findViewById(R.id.tvCategoyName) as TextView
    }

}

添加一个不可见的NavigationLink,如下所示:

@State private var showLinkTarget = false