fun with subclassing and Super

Coding Questions, IDE related or anything else

fun with subclassing and Super

Postby udo.killermann » Sat May 01, 2010 5:34 pm

Hello Board,

I have improved my code somehow and post the result. The reference manual tells, that Super doesn't work in the current implementation - you'll see it working quite the way expected:

UKWindow.NSWindow
Code: Select all
IBOutlet myButton As UKButton
Dim theTimeOut As Double

' we can make the compiler believe, that instances of UKButton
' receives messages that aren't defined in its implementation
' but are part of its parent's vocab
'
Declare Sub "UKButton" - (void)highlight:(BOOL)flag
Declare Function "UKButton" - (NSString *)title

Event Init()
  Alert("UKWindow init")
  theTimeOut = 8
End Event

' myButton isn't instanciated during the Init() event
' so I chose to use this event, that will surely be
' raised as soon as the window is brought to the
' foreground
Event WindowDidBecomeKey()
  myButton.setTimer(theTimeOut)
' message is received by myButton and passed up the chain
' cause myButton doesn't know how to handle it
  myButton.highlight(1)
End Event
 

IBAction ukButtonPressed(sender As id)
  ' Alert(sender)
  ' Alert("Button Pressed!")
  ' Alert(myButton.title())
  ' myButton.setTimer(theTimeOut)
 
' using an overrideen message can be fun ;-)
  myButton.setTitle("Huhn")
  myButton.performClick(myButton)
End IBAction



UKButton.NSButton
Code: Select all
Private Dim myDuration As Double

Private Dim myTimer As NSTimer

' in order to use Super invocations correctly, we need to introduce
' the needed messages by Declaring them
'
Declare Sub "NSButton" - (void)setEnabled:(BOOL)state
Declare Sub "NSButton" - (void)setTitle:(NSString *)title
Declare Sub "NSButton" - (void)performClick:(id)sender

' standard setter
Public Sub setTimer(theDuration As Double)
  myDuration = theDuration
  Log("setTimer")
  Log(myDuration)   
End Sub

' standard getter
Public Function getTimer() As Double
  Log("GetTimer")
  Log(myDuration)
  Return myDuration
End Function

' overriden message, the sent title will never be set
' it is alway overriden to "Title!" - you could do other
' things
Public Sub setTitle(theTitle As String)
  Log("The title")
  Log(theTitle)
  Super.setTitle("Title!")
End Sub

' Overriding action from parent NSButton
' just use the convention presented by Class Reference
'
' the idea is to disable the button until the set duration
' has elapsed
'
Sub performClick(sender As id)
  Log("performClick")
  Super.setEnabled(0)
  myTimer = NSTimer(myDuration)
  Super.performClick(sender)
End Sub

' the duration has elapsed, so we can reenable the button
'
Action myTimer()
  myTimer.Stop()
  Log("time is out!")
  Super.setEnabled(1)
End Action



You'll find the complete project attached.

Kind regards
Udo

BTW: It's very hard to upload a project within the board's filesize limit of 256KByte
Attachments
windowDecoration.zip
revisited project
(245.44 KiB) Downloaded 76 times
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: fun with subclassing and Super

Postby berndnoetscher » Tue May 04, 2010 11:36 am

Hi Udo,

thanks for your new example :-) . Will add it to the offical examples.

I changed the board's filesize limit now - it is much more.

CU
Bernd
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am


Return to Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 1 guest

cron