You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击按钮跳转至仅含TextView的另一个ViewController

Got it, let's walk through how to implement this step by step—super straightforward once you break it down!

实现点击按钮跳转至含TextView的ViewController

我会给你两种最常用的实现方式,按需选择就行:

方式一:用Storyboard(可视化快速实现)

1. 准备目标输入页面

  • 打开你的Storyboard,拖入一个新的UIViewController。
  • 拖一个UITextView到这个新VC上,调整约束让它铺满屏幕(或者按你想要的布局来)。如果需要占位提示,TextView默认没有这个功能,你可以拖个UILabel叠在上面当占位,或者用代码实现占位逻辑(后面会提)。
  • 给这个新VC设置Storyboard ID:选中它,在右侧Identity Inspector里,把Storyboard ID设为TextInputVC(选个好记的名字就行)。

2. 给按钮添加点击跳转逻辑

回到原ViewController,要么在Storyboard里按住Ctrl拖按钮到代码里创建IBAction,要么手动写点击事件:

// Swift 示例
@IBAction func openTextInputPage(_ sender: UIButton) {
    // 从Storyboard获取目标VC
    guard let textInputVC = storyboard?.instantiateViewController(withIdentifier: "TextInputVC") else {
        print("找不到对应的Storyboard ID,请检查配置")
        return
    }
    // 跳转方式二选一:导航栈push(如果原VC在导航栏里),或者模态弹出
    navigationController?.pushViewController(textInputVC, animated: true)
    // 模态弹出写法:present(textInputVC, animated: true)
}
// Objective-C 示例
- (IBAction)openTextInputPage:(UIButton *)sender {
    UIViewController *textInputVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TextInputVC"];
    [self.navigationController pushViewController:textInputVC animated:YES];
    // 模态弹出写法:[self presentViewController:textInputVC animated:YES completion:nil];
}

方式二:纯代码创建(无需Storyboard)

1. 创建目标输入页面类

新建一个TextInputViewController.swift(或Objective-C的.h/.m文件):

// Swift 示例
import UIKit

class TextInputViewController: UIViewController, UITextViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        // 创建TextView
        let inputTextView = UITextView()
        inputTextView.translatesAutoresizingMaskIntoConstraints = false
        inputTextView.font = .systemFont(ofSize: 16)
        // 实现占位文本效果
        inputTextView.text = "请输入内容..."
        inputTextView.textColor = .lightGray
        inputTextView.delegate = self
        
        // 添加到视图并设置约束(铺满安全区域)
        view.addSubview(inputTextView)
        NSLayoutConstraint.activate([
            inputTextView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            inputTextView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            inputTextView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            inputTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
    
    // 处理占位文本的编辑状态切换
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.text == "请输入内容..." && textView.textColor == .lightGray {
            textView.text = nil
            textView.textColor = .black
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = "请输入内容..."
            textView.textColor = .lightGray
        }
    }
}
// Objective-C 示例
// TextInputViewController.h
#import <UIKit/UIKit.h>

@interface TextInputViewController : UIViewController <UITextViewDelegate>

@end

// TextInputViewController.m
#import "TextInputViewController.h"

@interface TextInputViewController ()
@property (nonatomic, strong) UITextView *inputTextView;
@end

@implementation TextInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建TextView
    self.inputTextView = [[UITextView alloc] init];
    self.inputTextView.translatesAutoresizingMaskIntoConstraints = NO;
    self.inputTextView.font = [UIFont systemFontOfSize:16];
    self.inputTextView.text = @"请输入内容...";
    self.inputTextView.textColor = [UIColor lightGrayColor];
    self.inputTextView.delegate = self;
    
    [self.view addSubview:self.inputTextView];
    
    // 设置约束
    [NSLayoutConstraint activateConstraints:@[
        [self.inputTextView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
        [self.inputTextView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:16],
        [self.inputTextView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-16],
        [self.inputTextView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor]
    ]];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"请输入内容..."] && textView.textColor == [UIColor lightGrayColor]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        textView.text = @"请输入内容...";
        textView.textColor = [UIColor lightGrayColor];
    }
}

@end

2. 按钮点击跳转逻辑

回到原ViewController,添加按钮点击事件:

// Swift 示例
@IBAction func openTextInputPage(_ sender: UIButton) {
    let textInputVC = TextInputViewController()
    navigationController?.pushViewController(textInputVC, animated: true)
    // 模态弹出写法:present(textInputVC, animated: true)
}
// Objective-C 示例
- (IBAction)openTextInputPage:(UIButton *)sender {
    TextInputViewController *textInputVC = [[TextInputViewController alloc] init];
    [self.navigationController pushViewController:textInputVC animated:YES];
    // 模态弹出写法:[self presentViewController:textInputVC animated:YES completion:nil];
}

小提醒

  • 如果你的原ViewController不在导航栈里,一定要用present跳转,用push会报错哦。
  • 要是需要把输入的内容传回原页面,可以用闭包(Swift)或者代理(OC/Swift通用),需要的话我可以再补充这部分细节!

内容的提问来源于stack exchange,提问作者user10171513

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:43:49