How do I attach an action to a button using Interface Builder? - ios4

I am new to Interface Builder. I have a method (IBAction) defined in my class. I want to fire this method when a particular button is clicked.
I cant see any way to add an action via the IBBuilder

Setting up an Action
Right-Click on your control instance
Drag to your target and let go.
Setting up an Outlet
Right-Click on your object instance
Drag to your control instance and let go.
Inspecting Actions/Outlets/Bindings
Right-Click on your object instance

Have you defined your method as an IBAction? You need to do like this for the method appear in the "File Owner's".

Related

How to get state of checkbox of one class in another class vc++ mfc

I am developing MFC based SDI application in VC++ derived from CFormView class.In my dialog I have a checkbox. When this check box is clicked or not I want to get the state of this check box in main class say CDemoView.cpp and use that particular state for some calculations in another class say OServer.cpp which is a C++ class. I tried using SetCheck(), GetCheck() functions and I am failing.How can I get the state of checkbox in my other class??
Thanks in advance
Take a bool variable in the class.set its state according the checkbox.access that variable in other class to get the state of your check box.
The "other class" needs a variable that will let it access the view object that contains the check box state. Something like
if (pview->m_IsChecked)
or even
if(*pbool_IsChecked)
Initialize this pointer by passing the appropriate address to "other class" in its constructor or in a function you add for this purpose.

deriving Dialogs from a custom Dialog using mfc

I am trying to define a custom Dialog Template with certain members such as m_hImage, m_hName, and the m_hIcon in it. I want to then derive Dialogs using this Template.
I am not using any buttons(Ok,cancel etc.) in this template. Do i still need to declare or define OnOk(), OnCancel() etc.
Can anybody tell me what are the other methods I need to declare in this template class?
My goal is to just prepare this template dialog so that each dialog derived from this template contains a icon on the title bar (m_hIcon) , the image (m_hImage) on the upper left corner, and the image name (m_hName) on the upper right corner of dialog.
No, you need not to override any of the methods. You may however need to override OnInitDialog, otherwise you won't have any dialog initialization. This is the overridden method where you can setup the icon for dialog.

Bind UIElement to viewModel

I have a simple view containing a richtextbox and a button. I want to enter text into my RTB and on clicking my button have viewmodel print the RTB.
I have my command set up from the views print button and in my viewmodel have a UIElement property.
My question is how do I bind the RTB directly to my UIElement property in viewModel?
I'm fine with hooking individual properties of the RTB up but what about the whole control?
Not certain how you might accomplish that using databinding, how about just setting the reference manually?
MyControl.Loaded += (s, e) => {
((ViewModel)MyControl.DataContext).UiElementProperty = MyControl;
};
... although I'm not sure why you want to perform a task like that in the VM. How about just handling it in the view? Otherwise you might also encounter "dialogue must be user initiated" type errors.

Manually create a dialog child window using an ID from resource.h

I want to add a simple panel to a dialog created using Visual Studio resource editor, but the resource editor doesn't allow this - I need to add my own CWnd as a dialog child. However that way I think I have to use CWnd::Create manually, and pass in names for the class and the window.
I want to create an ID like IDC_MYPANEL, and as much as possible add the window so it works like something defined in the template. What's the right way to do this, and what's the best MFC class to use as a simple panel... just use CWnd itself?
What do you mean by 'a simple panel'? If it's a custom control, derive from CWnd, override Create() and call CWnd::Create() with NULL as the class name so that MFC makes its own, and add a line to resource.h with the IDC_XXX value of your control. If it's a sub-dialog, with controls on it, derive from CDialog and call CDialog::Create() with the IDD that you define in your dialog.
The only difference when creating a control at runtime is that in OnInitDialog, you do some Create() and initialisation things, and you don't include a DDX_Control() line for that control. For the rest everything works the same.

How does MonoTouch autogenerate XIB code behind?

I'm a C# programmer dabbling in a bit of iPhone development using MonoTouch.
I add a new View Interface Definition to my project and double click to open it up in Interface Builder. I add a UIButton. I save the file, and inspect the xib.designer.cs file, and I can see no reference to the new button.
I downloaded the code from http://monotouchexamples.com/ where I could see an example of autogenerated code behind :
[MonoTouch.Foundation.Connect("infoButton")]
private MonoTouch.UIKit.UIButton infoButton {
get {
return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("infoButton")));
}
set {
this.SetNativeField("infoButton", value);
}
}
I opened up MainWindow.xib in interface builder. I notice a few differences. File's Owner is of type UIApplication instead of NSObject. What is the importance of this? There is an App Delegate object of type AppDelegate. I can't add an AppDelegate to my own view, or at least I can't find it in the Library. Do I need to add one? I can see that the existing controls on MainWindow.xib have Referencing Outlets to the App Delegate. I add a new button and I want to hook it up. When I click and drag a New Referencing Outlet to the App Delegate a context menu appears that lists the existing controls. How do I add a new element to this list, or where does this list come from?
I've been spoilt by the Visual Studio world where I just dump a button on a form and start writing code for the click event. Could someone provide some pointers about the steps needed to get this working on MonoTouch?
Thanks,
Patrick
Adding a button by itself is not enough. The button is not accessible outside the Interface Builder. You need add an Outlet, and connect the button with the outlet in Interface Builder.
Remember: Outlets are the members in your Controller class that get a reference to the controls, you can't just access the controls without them.
As Dave says, you need to add an outlet to your controller class, and connect your button to that outlet, before any auto-generated code will appear. This caught me out too initially.
You choose your controller class in the Interface Builder library window, choose 'outlets' in the bottom part of the library, and add an outlet there. You then need to select your button, choose the connections tab of the inspector window, and drag from the "New referencing outlet" circle over to your controller class. Interface Builder will then prompt you to choose an outlet to connect to. Then when you save, you should get the auto-generated code appear in the .xib.designer.cs file, and then you should be able to reference your button via that outlet variable in your .xib.cs file.
It sounds like the project I created is out of date - I remember there were quite a few changes around how the generated buttons are created in the designer file. I will update the project soon for you.
As Dave said, to get the code to be auto generated you need to add an outlet with Interface Builder. There should be an example on this video here - http://bit.ly/aWoItN but the server seems to be down at the moment.
Hope this helps,
ChrisNTR

Resources