O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Click to Roll

Your users have been clamoring for a way to roll the die. The designer thinks that double-clicking on it would be a sensible way to accomplish this, so let’s add it. Add a randomize() method to DieView:

 ​ ​ ​ ​f​u​n​c​ ​r​a​n​d​o​m​i​z​e​(​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​V​a​l​u​e​ ​=​ ​I​n​t​(​a​r​c​4​r​a​n​d​o​m​_​u​n​i​f​o​r​m​(​5​)​)​ ​+​ ​1​
 ​ ​ ​ ​}

In mouseUp(_:), use NSEvent’s clickCount to check for the double-click before calling randomize():

 ​ ​ ​ ​o​v​e​r​r​i​d​e​ ​f​u​n​c​ ​m​o​u​s​e​U​p​(​t​h​e​E​v​e​n​t​:​ ​N​S​E​v​e​n​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​l​n​(​"​m​o​u​s​e​U​p​ ​c​l​i​c​k​C​o​u​n​t​:​ ​\​(​t​h​e​E​v​e​n​t​.​c​l​i​c​k​C​o​u​n​t​)​"​)​
 ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​t​h​e​E​v​e​n​t​.​c​l​i​c​k​C​o​u​n​t​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required