如何以编程方式打开一个新的ViewController?

时间:2018-07-25 13:30:33

标签: ios swift xcode uiviewcontroller

我是iOS开发的新手。我已经将UIView视为按钮,并创建了一个函数,该函数告诉您在点击时该怎么做。现在,我想在点击时打开一个名为“ newView”的新ViewController。是否可以不使用故事板(仅通过编程方式)?

enter image description here

2 个答案:

答案 0 :(得分:0)

let newController = ViewController()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = newController

或者如果您想使用情节提要的参考来打开它

let storyboard = UIStoryboard(name: "SecondViewController", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
self.present(controller, animated: true, completion: nil)ode here

答案 1 :(得分:0)

是的,如果有导航堆栈,则可以推送。像下面一样

import React from 'react'
import Typography from '@material-ui/core/Typography'
import { withStyles } from '@material-ui/core/styles'
import { List, ListItem, ListItemText } from '@material-ui/core';

const styles = theme => ({
  title: {
    padding: theme.spacing.unit*1.5,
  },
}

const LogComponent = ({classes, list_class, heading, lines}) => {

    return (
    <div className={classes.root}>
    <Typography className={classes.title} color="textSecondary" key={1}>
            {heading}
            </Typography>
            <div>
            <List dense>
                {[...lines[0]].map(e => 
                <ListItem><ListItemText primary={e} /></ListItem>
                )}
            </List>
            </div>                    
    </div>
    )
}

export default withStyles(styles)(LogComponent)

如果没有navigationController堆栈,则使用下面的堆栈。

let newController = ViewController()
self.navigationController?.pushViewController(newController, animated: true)