|
1503 | 1503 | }
|
1504 | 1504 | </pre>
|
1505 | 1505 | <p>
|
1506 |
| - Nothing too crazy here. You’re simply doing the following: |
| 1506 | + 这里没有太多累赘的东西。你只需去完成下列的事情: |
1507 | 1507 | </p>
|
1508 | 1508 | <ol>
|
1509 | 1509 | <li>
|
1510 |
| - Creating a new beer. |
| 1510 | + 创建一个新的啤酒。 |
1511 | 1511 | </li>
|
1512 | 1512 | <li>
|
1513 |
| - Inserting the beer into the model. |
| 1513 | + 将啤酒插入到model中。 |
1514 | 1514 | </li>
|
1515 | 1515 | <li>
|
1516 |
| - Inserting a new row into the table. |
| 1516 | + 为table插入一个新的行。 |
1517 | 1517 | </li>
|
1518 | 1518 | <li>
|
1519 |
| - Selecting the row of the new beer. |
| 1519 | + 选择新啤酒所在的这行。 |
1520 | 1520 | </li>
|
1521 | 1521 | </ol>
|
1522 | 1522 | <p>
|
1523 |
| - You might have even noticed that, like in iOS, you need to call |
| 1523 | + 你货主注意到了这点,就像在iOS中一样,你需要在插入新的行之前调用 |
1524 | 1524 | <code>
|
1525 | 1525 | beginUpdates()
|
1526 | 1526 | </code>
|
1527 |
| - and |
| 1527 | + 和 |
1528 | 1528 | <code>
|
1529 | 1529 | endUpdates()
|
1530 | 1530 | </code>
|
1531 |
| - before inserting the new row. See, you really do know a lot about macOS |
1532 |
| - already! |
| 1531 | + 。所以说,你其实早已懂得了关于macOS的很多的内容! |
1533 | 1532 | </p>
|
1534 | 1533 | <h3>
|
1535 |
| - Removing Entries |
| 1534 | + 移除条目 |
1536 | 1535 | </h3>
|
1537 | 1536 | <p>
|
1538 |
| - To remove a beer, add the below code for |
| 1537 | + 为了移除一瓶啤酒,添加下列的代码到 |
1539 | 1538 | <code>
|
1540 | 1539 | removeBeer(\_:)
|
1541 | 1540 | </code>
|
1542 |
| - : |
| 1541 | + 中: |
1543 | 1542 | </p>
|
1544 | 1543 | <pre lang="swift" class="language-swift hljs"><span class="hljs-meta">@IBAction</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">removeBeer</span><span class="hljs-params">(<span class="hljs-number">_</span> sender: Any)</span></span> {
|
1545 | 1544 | <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> beer = selectedBeer,
|
|
1561 | 1560 | }
|
1562 | 1561 | </pre>
|
1563 | 1562 | <p>
|
1564 |
| - Once again, very straightforward code: |
| 1563 | + 依然是非常直接的代码: |
1565 | 1564 | </p>
|
1566 | 1565 | <ol>
|
1567 | 1566 | <li>
|
1568 |
| - If a beer is selected, you remove it from the model. |
| 1567 | + 如果已选中一个啤酒,就从model中移除它。 |
1569 | 1568 | </li>
|
1570 | 1569 | <li>
|
1571 |
| - Reload the table view, and select the first available beer. |
| 1570 | + 重载table view,选择第一瓶可用的啤酒。 |
1572 | 1571 | </li>
|
1573 | 1572 | </ol>
|
1574 | 1573 | <h3>
|
1575 |
| - Handling Images |
| 1574 | + 处理图片 |
1576 | 1575 | </h3>
|
1577 | 1576 | <p>
|
1578 |
| - Remember how |
| 1577 | + 记得 |
1579 | 1578 | <em>
|
1580 | 1579 | Image Wells
|
1581 | 1580 | </em>
|
1582 |
| - have the ability to accept an image dropped on them? Change |
| 1581 | + 拥有接收拖拽到它上面的图片的能力么?将 |
1583 | 1582 | <code>
|
1584 | 1583 | imageChanged(\_:)
|
1585 | 1584 | </code>
|
1586 |
| - to: |
| 1585 | + 的方法修改为: |
1587 | 1586 | </p>
|
1588 | 1587 | <pre lang="swift" class="language-swift hljs"><span class="hljs-meta">@IBAction</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">imageChanged</span><span class="hljs-params">(<span class="hljs-number">_</span> sender: NSImageView)</span></span> {
|
1589 | 1588 | <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> image = sender.image <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> }
|
1590 | 1589 | selectedBeer?.saveImage(image)
|
1591 | 1590 | }
|
1592 | 1591 | </pre>
|
1593 | 1592 | <p>
|
1594 |
| - And you thought it was going to be hard! Apple has taken care of all the |
1595 |
| - heavy lifting for you, and provides you with the image dropped. |
| 1593 | + 你以为这可能会很难!但苹果早已为你负责处理了所有繁重的工作,并将接收拖拽来的图片的能力赐予了你。 |
1596 | 1594 | </p>
|
1597 | 1595 | <p>
|
1598 |
| - On the flip side to that, you’ll need to do a bit more work to handle |
1599 |
| - user’s picking the image from within your app. Replace |
| 1596 | + 但另一方面,你需要做很多工作,来方便用户从你的app中选取图片。将 |
1600 | 1597 | <code>
|
1601 | 1598 | selectImage()
|
1602 | 1599 | </code>
|
1603 |
| - with: |
| 1600 | + 方法替换为: |
1604 | 1601 | </p>
|
1605 | 1602 | <pre lang="swift" class="language-swift hljs"><span class="hljs-meta">@IBAction</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">selectImage</span><span class="hljs-params">(<span class="hljs-number">_</span> sender: Any)</span></span> {
|
1606 | 1603 | <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> window = view.window <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> }
|
|
1628 | 1625 | }
|
1629 | 1626 | </pre>
|
1630 | 1627 | <p>
|
1631 |
| - The above code is how you use |
| 1628 | + 上述代码实现了你使用 |
1632 | 1629 | <code>
|
1633 | 1630 | NSOpenPanel
|
1634 | 1631 | </code>
|
1635 |
| - to select a file. Here’s what’s happening: |
| 1632 | + 来选取一个文件的过程。以下是详细步骤: |
1636 | 1633 | </p>
|
1637 | 1634 | <ol>
|
1638 | 1635 | <li>
|
1639 |
| - You create an |
| 1636 | + 创建一个 |
1640 | 1637 | <code>
|
1641 | 1638 | NSOpenPanel
|
1642 | 1639 | </code>
|
1643 |
| - , and configure its settings. |
| 1640 | + ,并配置它的设置。 |
1644 | 1641 | </li>
|
1645 | 1642 | <li>
|
1646 |
| - In order to allow the user to choose only pictures, you set the allowed |
1647 |
| - file types to your preferred image formats. |
| 1643 | + 为了让用户只可以选择图片,你将允许的文件类型设置为你所需的文件格式。 |
1648 | 1644 | </li>
|
1649 | 1645 | <li>
|
1650 |
| - Present the sheet to the user. |
| 1646 | + 展示这个sheet给用户。 |
1651 | 1647 | </li>
|
1652 | 1648 | <li>
|
1653 |
| - Save the image if the user selected one. |
| 1649 | + 如果用户选择了一张图片的话,保存它。 |
1654 | 1650 | </li>
|
1655 | 1651 | </ol>
|
1656 | 1652 | <p>
|
1657 |
| - Finally, add the code that will save the data model in |
| 1653 | + 最后,在 |
1658 | 1654 | <code>
|
1659 | 1655 | updateBeer(\_:)
|
1660 | 1656 | </code>
|
1661 |
| - : |
| 1657 | + 中添加保存数据model的代码: |
1662 | 1658 | </p>
|
1663 | 1659 | <pre lang="swift" class="language-swift hljs"><span class="hljs-meta">@IBAction</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">updateBeer</span><span class="hljs-params">(<span class="hljs-number">_</span> sender: Any)</span></span> {
|
1664 | 1660 | <span class="hljs-comment">// 1.</span>
|
|
1679 | 1675 | }
|
1680 | 1676 | </pre>
|
1681 | 1677 | <p>
|
1682 |
| - Here’s what you added: |
| 1678 | + 上述代码: |
1683 | 1679 | </p>
|
1684 | 1680 | <ol>
|
1685 | 1681 | <li>
|
1686 |
| - You ensure the beer exists, and update its properties. |
| 1682 | + 确认啤酒是存在的,并更新它的property。 |
1687 | 1683 | </li>
|
1688 | 1684 | <li>
|
1689 |
| - Update the table view to reflect any names changes in the table. |
| 1685 | + 更新table view以在table上反映任何名称的变化。 |
1690 | 1686 | </li>
|
1691 | 1687 | <li>
|
1692 |
| - Save the data to the disk. |
| 1688 | + 保存数据到磁盘上。 |
1693 | 1689 | </li>
|
1694 | 1690 | </ol>
|
1695 | 1691 | <p>
|
1696 |
| - You’re all set! Build and run the app, and start adding beers. Remember, |
1697 |
| - you’ll need to select |
| 1692 | + 你已经全部设定完毕!运行app,然后添加啤酒。记住你需要选择 |
1698 | 1693 | <em>
|
1699 | 1694 | Update
|
1700 | 1695 | </em>
|
1701 |
| - to save your data. |
| 1696 | + 来更新你的数据。 |
1702 | 1697 | </p>
|
1703 | 1698 | <p>
|
1704 | 1699 | <img src="https://koenig-media.raywenderlich.com/uploads/2017/05/BeerTracker-mac-UI-Beers.png"
|
|
1707 | 1702 | sizes="(max-width: 600px) 100vw, 600px">
|
1708 | 1703 | </p>
|
1709 | 1704 | <h3>
|
1710 |
| - Final Touches |
| 1705 | + 最后的接触 |
1711 | 1706 | </h3>
|
1712 | 1707 | <p>
|
1713 | 1708 | You’ve learned a lot about the similarities and differences between iOS
|
|
0 commit comments