Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 22, 2011, 10:44 PM   #1
larswik
macrumors 65816
 
Join Date: Sep 2006
Open a second window and pause the code?

I have a button that triggers a method. When the method is executed I have a second window pop up that asks for the level of the creature and if he was killed or still alive to do some math. When the second window opens I want the code in the method to pause or wait until the new information is entered so it can do the math correctly adding bonuses and what not.

I thought this might work but I get a beach ball of death.
Code:
- (IBAction)endCombatButton:(id)sender {
    doaWindowOpen = YES;
    [doaWindow makeKeyAndOrderFront:self];
    while (doaWindowOpen) {
        ;
    }
With the close button on the second window making the doaWindowOpen = NO; which should exit the loop and continue the code.
Code:
- (IBAction)doaWindowCloseButton:(id)sender {
    doaWindowOpen = NO;
    [doaWindow orderOut:self];
}
Is there some code I am unaware of that will hold the rest of the method from executing until I close the second window?
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Aug 22, 2011, 11:06 PM   #2
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
I thought of a solution that should work. I took the rest of the code that was supposed to execute and pretty much copy and pasted it the the 'close button' method for the second window. Then on the original button I just left the open the second window code. When the second window closes the rest of the code gets executed.

If there is a to pause the code I would love to know since I am learning.

Thanks
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Aug 23, 2011, 01:05 AM   #3
SidBala
macrumors 6502a
 
Join Date: Jun 2010
I don't know about the mac or cocoa specific solution. But you should use some kind of thread synchronization object. This is a much better solution than polling a bool.

In windows, there are Events you can create and wait on. This will "pause" code running on one thread until you release it from another thread(the UI thread).

Running through the docs really quickly reveals NSConditionLock. This might be what you need.
SidBala is offline   0 Reply With Quote
Old Aug 23, 2011, 01:19 AM   #4
foidulus
macrumors 6502a
 
Join Date: Jan 2007
The correct term for what you are looking for is a modal window, ie a window that prevents any other window from doing anything till the modal window is dealt with.

Here is a good stack overflow page on the topic.

http://stackoverflow.com/questions/1...oa-objective-c

Last edited by foidulus; Aug 23, 2011 at 01:21 AM. Reason: read the question better
foidulus is offline   0 Reply With Quote
Old Aug 23, 2011, 02:54 AM   #5
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Thanks! I will take a look at the link.

Obj -C is so vast. I spent the last 45 minutes now looking for the information window that displays it's self when you hover over a button or object that explains that tool.

Thanks again!
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Aug 23, 2011, 03:25 AM   #6
foidulus
macrumors 6502a
 
Join Date: Jan 2007
Quote:
Originally Posted by larswik View Post
Thanks! I will take a look at the link.

Obj -C is so vast. I spent the last 45 minutes now looking for the information window that displays it's self when you hover over a button or object that explains that tool.

Thanks again!
Well, to be pedantic its not really Objective-C that has all the windowing stuff, it is Cocoa which is built on top of Objective-C. Objective-C is a programming language, Cocoa a framework.
foidulus is offline   0 Reply With Quote
Old Aug 23, 2011, 03:44 AM   #7
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Ya yes, that is true. I am starting my Java class at City College this Wednesday. The one thing about the last Pascal class I took was the interaction with other programmers and getting the lingo and proper nomenclature, which is hard to develop learning by myself at home.

Thanks.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Aug 23, 2011, 08:34 AM   #8
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by larswik View Post
I am starting my Java class at City College this Wednesday. The one thing about the last Pascal class I took was the interaction with other programmers and getting the lingo and proper nomenclature, which is hard to develop learning by myself at home.
Good luck with the Java class. I'm tutoring a friend thought a masters class in Java at the moment. Java from 0 to 60 mph in 10 weeks.

Nomenclature is definitely a challenge, especially when we as computer scientists overload terms, like object. But it is vary important to get it right. You do need to be able to communicate effectively with your future colleagues, and the lingo goes a long a way to facilitating this.
jiminaus is offline   0 Reply With Quote
Old Aug 23, 2011, 12:43 PM   #9
wlh99
macrumors 6502
 
Join Date: Feb 2008
Your soution is a good one, and a Modal window would work too. I thought I would give a quick explaination of why you got a beach ball though, as it is part of how Cocoa works.

Cocoa is event based. Everything happens in relationship to an event. For example, when you click a button, and an event happens. There are other events too.

The catch is that only one line of code can execute at a time. (unless your program is multithreaded, which is a more advanced topic)

In the operating system, there is an event loop, that waits for an event, then when one occurs it calls the appropriate code. When that code is done executing, control returns to the event loop, waiting for the next event.

Usually this is invisible, a method will execute and return control to the event loop so fast you never notice it. But if you create a while loop like you did, control never returns to the event loop. So events can not be detected in you new window, and it is imposible for the BOOL condition to change. So it truly turns into an infinate loop, and OS/X throughs up a beach ball.
__________________
Warren Holybee
wlh99 is offline   0 Reply With Quote
Old Aug 23, 2011, 02:38 PM   #10
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Thanks for the explanation. I have another part in the code where I need a window called. I think I will call the Modal window approach for this one.

I have to say, the more I learn about programming the more fun it is becoming. Last night I sat down at 6p and stopped at 3am. I even skipped having dinner last night and just kept working.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Aug 23, 2011, 07:49 PM   #11
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
You are not in control

In Cocoa, you are not in control. The run loop is in control. It does what it wants when it wants. Your code is not in control. Your code runs within the (invisible) run loop. The run loop calls methods in your code (and other methods that you don't see).

To answer your specific question, foidulus is correct: you want a model window.

On a larger scale, wlh99 is right. My first paragraph above reinforces this.

Do not fight Cocoa. You will lose. Accept your place in the universe (below Cocoa, above single-celled organisms).
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Open file in different window dbutlerk OS X 10 Mar 12, 2014 12:47 PM
How to open new finder window to last location tmanto02 OS X 10.8 Mountain Lion 0 May 16, 2013 09:25 PM
Why does Finder open so tiny how can I set it to open in bigger window? bobright OS X 2 May 6, 2013 12:17 PM
Watching TV/Movies from iTunes and pause, window resizes walterhpdx Mac Applications and Mac App Store 0 Dec 22, 2012 01:02 PM
Tree click-open in new window Husky1992 MacBook Pro 1 Oct 29, 2012 02:44 PM

Forum Jump

All times are GMT -5. The time now is 08:47 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC