如何手动触发UIBarButtonItem点击事件?

时间:2020-06-14 05:59:30

标签: ios swift uibutton uibarbuttonitem uitabbar

在这种情况下,我有一个自定义UIButton,它是UIBarButtonItem的子视图:

guard let tab1 = self.tabBar.items![0].value(forKey: "view") as? UIView else {return}

    let button2Test = UIButton()
    tab1.addSubview(button2Test)

自定义UIButton具有一个目标:

button2Test.addTarget(self, action: #selector(buttonTouchUpInsideP3), for: [.touchUpInside])
button2Test.addTarget(self, action: #selector(buttonTouchInsideBoundsP3), for: [.touchDown, .touchDragEnter])
button2Test.addTarget(self, action: #selector(buttonDraggedOutOfBoundsP3), for: [.touchDragExit, .touchCancel])

基本上,在buttonTouchUpInsideP3函数中,我想以编程方式/手动使UIBarButtonItem在特定的索引上好像被轻按进行触发,因为UIButton的目标不允许实际不幸的是,UIBarButtonItem在下面被窃听。

由于错误而无法执行的尝试是:(我从outdated Q获得)。请注意,我在buttonTouchUpInsideP3目标函数中调用了该函数,放开按钮后即调用该函数。

let homeTabBarItem = self.tabBar.items![0]
UIApplication.sharedApplication.sendAction(homeTabBarItem.action, to: homeTabBarItem.target, from: nil, forEvent: nil)

2 个答案:

答案 0 :(得分:0)

您可以直接设置UIBarButtonItem的target和action属性。

答案 1 :(得分:0)

我想出了办法:

*, *::after, *::before{
    box-sizing: border-box;
}

nav {
    background-color: black;
    color: white;
    height: 35px;
    position: relative;
}

p {
    position: absolute;
    margin: 0;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 35px;
}

table {
    width: 100%;
}

th {
    text-align: left;
}

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}

th, td {
    padding: 10px;
}



.modal-bg{
    position: fixed;
    width: 100%;
    height: 100vh;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    visibility: hidden;
    opacity: 0;
    transition: visibility 0s, opacity 0.5s;
}

.bg-active{
    visibility: visible;
    opacity: 1;
}




.modal{
    position: relative;
    padding: 10px;
    font-family: "montserrat", sans-serif;
    background-color: white;
    width: 30%;
    height: 30%;
    display: flex;
    justify-content: space-around;
    align-content: center;
    flex-direction: column;
}

.modal-button{
    padding: 10px 30px;
    background-color: black;
    color: white;
    border: none;
    font-family: "Montserrat", sans-serif;
    cursor: pointer;
}

.modal-close{
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
}

.submit-button{
    margin-top: 10px;
    margin-bottom: 10px;
}

只需将其放在buttonTouchUpInside函数中即可! ;)

您可能还应该致电:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src = "script.js" defer></script>
    <link rel = "stylesheet" href="styles.css">
    


</head>
<body>
    <nav>
        <p>Library</p>
    </nav>
    <div class = displaytable>
        <table id = "display">
            <tr>
                <th>Title</th>
                <th>Author</th>
                <th>Pages</th>
                <th>Read</th>
                <th>Remove</th>
            </tr>
        </table>
    </div>
    


    <button class="modal-btn">Add Book</button>
    <button class = "delete-btn">Delete Book</button>


    <div class="modal-bg">
        <div class ="modal">
            <h2>Book</h2>

            <label for="name">Title</label>
            <input type="text" name="name" id = "titles">

            <label for="name">Author</label>
            <input type="text" name="name" id = "authors">

            <label for="name">Pages</label>
            <input type="text" name="name" id = "pages">

            <label for="name">Read</label>
            <input type="text" name="name" id = "reads">
            
            <span class="modal-close">x</span>

            <button class="submit-button" id = "subDisplay" onclick="addBookToLibrary()">Submit</button>
        </div>
    </div>

    
    
</body>
</html>
相关问题