I added an UIButton to my GameViewController in storyboard and set it to hidden.
Now I want to let that button show up from the GameScene
I have set the Restoration ID to "sharee" and I also set the Document label to "sharee", but when I use sharee.hidden = false in my GameViewController or GameScene, it says: "use of unresolved identifier sharee".
How can I interact with the UIButton I added in storyboard from my GameScene?
First you want to make a reference to the button when you assign the scene. You want to make sure you create an IBOutlet from your storyboard to your view controller as well.
In your game's view controller before you present the scene.
yourGameScene.button = _yourViewControllerButton;
Declare the button reference in your scene header.
#property UIButton *button;
Then in your implementation of the scene make the button hidden by typing...
_button.hidden = YES;
And make it appear by typing...
_button.hidden = NO;
Edit in more detail:
First you want to make sure that you have an IBOutlet connected to your view controller. The easiest way for someone starting out to do this is by switching to the assistant editor and then holding control button down and then clicking on your game button and dragging it into the interface section of the game view controller class. It will make a line. (Make sure it is the right view controller on your second view and make sure to choose outlet, not action, if it prompts.)
Secondly you want to add a property in your game scene header. You can name it whatever you want but for the tutorial sake just name it button and change it around later if you wish.
#property UIButton *button;
Third, when you created this game scene and started assigning properties, go ahead and set the view controller button reference to the game scene button reference.
Finally, do whatever you want to implement the hiding and the showing. For me I made it by toggling it in the touchesBegan method.
Related
Why can't I connect outlets from subviews?
Dropped a UIViewcontroller onto storyboard
Dropped a subview (UIView) as a custom class CustomSubView : UIView
Went into Inspector while subview is selected and changed custom class to CustomSubView
I have a couple of labels inside the subview
I tried to drag-drop from storyboard to assistant editor while CustomSubView class header is shown, but I don't get the pop-up to create the outlet. What gives?
I am using Xcode 5.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
It was answered here: Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
or you can try this:
After typing the property outlet declarations manually in the customview.h file I could ctrl-drag-connect them from there to their corresponding UIlabel objects in the interfacebuilder. Works only in this direction!
I have a StoryBoard and a base ViewController include some images.
How I can touch an image of them to go to other ViewController linked that UIImageView by modal?
create the new viewController
drag a tap gesture recognizer to the first viewController
right click + drag from the gesture recognizer to the new viewController and you will see the option modal
right click + drag from the UIImageView to the gestureRecognizer and you will see the gestureRecognizer option select it and that's all
Make sure the UIImageView have the "User Iteraction Enabled" option ckecked
just added a sample project to my github
You can try the following to move from one view controller to another:
[MyImageView.view removeFromSuperview];
[MyNewViewController.view addSubview:MyImageView.view];
If you wish to animate that change making it fly its way, you might need to create animation for MyImageView.view.frame from the old to the new view controller. For that you'll need to use methods like
CGRect fromFrame = [MyImageView.view convertRect:MyImageView.view.frame toView:MyOldViewController]
How about using buttons instead of images?
http://www.cocoanetics.com/2010/02/uiimageview-touch-handling-uibutton/
I have two views and i used two view controllers. From the main view i want to initialize the button in the second view with a certain text.
If I press the button in the second view, then i can store that button reference to a global UIButton, and change the text then onwards.
But the very first time, how can I initialize the button text in the second view from my main view with a particular text ?
You should not do that: every controller should be responsible for its own view. Full stop.
You can always set some poperty on the second controller and use it when the second view appears on the screen to change whatever you like.
you use delegates & #protocol to access other class example,
refer ex, ex1, link
Not sure how you did this. But if you post some code, it will be helpful. If you already have the above code working, then you need to have the following in order for what you are expecting to work.
Say you have ViewControllerA, ViewControllerB as the two view controllers. You can definitely initialize the text in the button when you create the XIB file.
But I am assuming that you want to do this programatically. If you want to access a button in another view controller, you need
a. Access to the other view controller.
b. Once you have access to the other view controller, access to the UIButton variable in that view controller.
So you can do the following in the ViewControllerA code,
-(void)changeButtonTextOfVCB{
viewControllerB.button setTitle:#"MyTitle" forControlState...
}
and you can have a button in viewControllerA, which will trigger the above method.
Having said this, I am not sure whether you have the whole thing working, where you can even switch between the view controllers
If you make a button in FirstViewController and want to change button properties like title..etc from the second view, then you should make properties in SecondViewController
#property (nonatomic,assign) UIButton *button;
when you navigate firstViewController to secondViewController just pass the button Object like
obj = [SecondViewController alloc]...]
obj.button = button;//(FirstViewController button object)
I am trying to put a button in the corner of my MKMapView to control whether the map stays locked on to the user's location. What I have in mind is to create a UIView with a button on and add it over my MKMapView (not as an annotation or something) I can't figure this out with Interface Builder.
How can I add this button programmatically?
Controlling whether it actually follows the user etc. is already sorted - just need the button for it.
It looks like you are directly setting the controller's view outlet to an MKMapView object rather than a UIView object containing the MKMapView object. You cannot drop the button in such case on top of the MKMapView object in the IB. There are two ways you can deal with this,
Declare an outlet for the button and drop the button in the IB. This needn't be on top of the MKMapView object. Set the outlet to, say, a button property. Then in viewDidLoad do [self.view addSubview:self.button]; after setting the button's frame. (or)
Drop a new UIView object in IB and put the MKMapView object inside it. Set the controller's view to this container UIView object. Later drop the button on top of the MKMapView object and set it to its appropriate location.
I am strugling on the following task.
I am trying to control my subview from another viewController class.
What I did and does not work is this.
I inserted an object and changed it class to my second viewController class.
Then I connected its UIButton outlet to a button I have on my subview.
I then connected the buttons action to the outlet of my second view controller.
What I get when I run is this.
It all shows up well but when I try to touch the button that resides in my subview app crashes. I am only left with a worringing: "Action unavailable: The "Touch Up Inside" event of "Rounded Rect Button".
It's probably my logic that is incorrect. Thanks for help.
Well after a long research I got an answer to my problem.
As it appears I was doing everything right.
The problem is that after the initial XIB file is initialized it autoreleases all views and subviews. So to prevent from gettig your second view controller from being released implement this method
- (void)awakeFromNib {
[self retain];
}
in your view controller .m file.
This method will retain your second view controller alive and allow it to recive and respond to UI actions.