【问题标题】:Using uitextviewcontroller to go to another view使用 uitextview 控制器转到另一个视图
【发布时间】:2013-05-20 00:35:08
【问题描述】:

所以我希望能够做的是单击一个按钮,弹出一个 tableViewController,然后单击其中一个条目将我带到一个新视图。

我已经使用从 NSArray 获得的正确条目设置了 tableView 弹出窗口。

我只是不知道如何制作它,以便当我点击一个条目时,它会将我带到我想要的视图。我相信它与以下默认功能有关:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

但我不确定。我有一个 NSArray *array of entries 和一个 MyViewController 类,所以如果你能帮我做一些类似下面的事情,那就太好了:

if(array entry 1) go to MyViewController1
if(array entry 2) go to MyViewController2 
etc...

【问题讨论】:

    标签: ios objective-c model-view-controller uitableview


    【解决方案1】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    当你点击 tableview 时被调用。方法中的indexpath 为你提供你选择的单元格

    所以indexpath.row 为您提供了应该查看的数组条目。amke 上的逻辑

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     int selectedEntry=indexpath.row;
     switch (selectedEntry) {
            case 1:
                //goto viewcontroller 1
                break;
            case 2:
                //goto viewcontroller 2
                break;
            default:
                break;
        }
    }
    

    推送方法

    使用storyboard

    【讨论】:

    • 我明白了,谢谢!但是我仍然对如何将视图更改为下一个视图控制器感到困惑。比如我将如何实现你注释掉的 switch 语句中的行?
    • 听上去好像他没有先将根视图控制器嵌入到 UINavigationController 中就创建了他的应用程序,否则,对于每个 switch 语句,这将是一个简单的 [self. navigationController pushViewController:viewController2 动画:YES];
    • 如何嵌入根视图控制器?
    【解决方案2】:

    这是你必须做的事,我的朋友: 选择行时会调用此委托方法。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
         switch (indexpath.row) {
                case 1:
                    {
                    ViewController1 *VC1=[ViewController1 alloc]init];
                    [self.navigationController pushViewController:VC1 animated:No];
                     break;
                    }
               case 2:
                   {
                    ViewController2 *VC2=[ViewController2 alloc]init];
                    [self.navigationController pushViewController:VC2 animated:No];
                    break;
                   }
              default:
                    break;
            }
        }
    

    希望这可以帮助你我的朋友..

    【讨论】:

    • 显然,只有当我首先将根视图控制器嵌入 UINavigationController 时,这种方法才有效……但我也不知道该怎么做。这是我在 UITableViewController 中也做的事情吗?
    • 你只需要使用 UINavigationController 的引用而不是 Self.navigationController 我的朋友例如:一个引用 UINavigationController *navi;那么你必须像这样使用 [navi pushViewController:VC2 animated:No];
    • 所以我在 .h 文件中声明并初始化 UINavigationController *navi 作为属性,然后在 viewDidLoad() 中将其初始化为 navi = [[UINavigationController alloc]init] ,然后在 didSelectRowAtIndexPath( ) 我写 ViewController1 *VC1=[ViewController1 alloc]init]; [self.navi pushViewController:VC1 动画:否];但这也不起作用。
    • 在这个委托中添加一个断点并首先检查它是否工作?
    【解决方案3】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
         int selectedEntry=indexpath.row;
         switch (selectedEntry) {
                case 1:
                    ViewController1 *vc = [[ViewController1 alloc] init];    
                    // Push View Controller 1 onto Navigation Stack
                    [self.navigationController pushViewController:vc animated:YES];
                    break;
                case 2:
                    ViewController2 *vc2 = [[ViewController1 alloc] init];    
                    // Push View Controller 2 onto Navigation Stack
                    [self.navigationController pushViewController:vc2 animated:YES];
                    break;
                default:
                    break;
            }
        }
    

    【讨论】:

    • 你也可以像这样展示模态:[self presentViewController:vc animated:YES completion:nil];
    【解决方案4】:
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if(indexPath.row==1)
    {
    //Go to view
    }
    else if(indexPath.row==2)
    {
    //Go to view
    }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      相关资源
      最近更新 更多