Reuse UIControl subclass with the design made in IB - iphone

I have subclassed UIControl to create my custom control, but I have designed it using Interface Builder. Now I want to "export" it and use it for another application and I don't know exactly what I should do in order to be able to reuse it. It loads using initWithCoder.
I have searched quite a bit on the Internet, but I haven't find a right answer for my question.
Thank you.

Maybe you need use the following code in viewDidLoad of ViewController method:
CustomControl *control = [[[NSBundle mainBundle] loadNibNamed:#"CustomControl" owner:nil options:nil] objectAtIndex:0];
control.frame = CGRectMake(5.0f, 0.0f, 310.0f, 180.0f);
[self.view addSubview:control];

Related

How to init UIView from xib using Swift?

Does anyone sucessfully replicate this objc method in swift? I still can't make it successful in init method.
self=[[[NSBundle mainBundle] loadNibNamed:#"DGNodeView" owner:nil options:nil] firstObject]
I still can't find a good answer that is clearly working with xcode 6.0.1

Problems when loading another UIView

In my app, I implemented a QR code reader that directs the app user to another View in the app after scanning any QR code.
I have two Xibs. One is the main screen that loads up when opening the app called demoViewController. The other is the page the app takes you to after scanning a QR code, called yViewController.
On yViewController, I masked the entire page using a UIView called sPage. The reason why I created this UIView is because I needed to implement this code:
[[NSBundle mainBundle] loadNibNamed:#"yViewController" owner:self options:nil];
[self.view addSubview:sPage];
Thats code that takes the user to the second page after scanning. I needed the mask UIView sPage in order to make this method work. And it does work.
However, I think this is screwing up my interface. I had a UIImageView in my sPage View implemented via the
demoViewConroller.m using viewDidLoad:
[topImage setImage: [UIImage imageNamed:#"ylogoREAL.png"]];
The image does not show up.
Additionally, I have some code to animate my view when the keyboard comes up and hides a textfield. This also does not work.
I know the hierarchy is right, because I use a buttonPressed method that works on the sPage View, as well as a method that closes the keyboard when touching the view. But nothing else works.
Can anyone help me figure out why? Im so confused :(
If I understand your error correctly you are calling a function and then ending the line with a semi-colon (programmers force of habit) then going to the next line (maybe) to open the function with a curly bracket.
-(void) function();
{
It needs to not have the semicolon
-(void) function()
{
I'm a bit confused as to what you're trying to achieve, but I'll give it a go
How do you initialize yViewController? I believe you should be using
yViewController = [[UIViewController alloc] initWithNibName:#"yViewController" bundle:[NSBundle mainBundle]];
instead of creating that mask view which loads up yViewController's nib.
Update:
After looking at your code, I believe you want to show a survey page after scanning is done. There are two things you can do
Option 1 - Easier
Present a UIView to the user, using your existing UIViewController (yViewController)
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:#"Survey" owner:self options:nil];
UIView *view_to_show = [nibs objectAtIndex:0];
[self.view addSubview:view_to_show];
Bear in mind that to be able to dismiss this view later, you will need a reference to it (declare it as a property) and later you can just do [view_to_show setHidden:YES]; or removeFromSuperview
Option 2 - UIViewController option
Create a new UIViewController with your survey view and show that
UIViewController *vc_new = [[UIViewController alloc] initWithNibName:#"Survey" bundle:[NSBundle mainBundle]];
[self presentViewController:vc_new animated:YES completion:nil];
All I needed to do was declare the topImage right under [[NSBundle mainBundle] loadNibNamed:#"yViewController" owner:self options:nil]; . I guess ViewDidLoad was the wrong place to put it in this case!

"Plays Sound" Property of UIButton in Interface Builder

in interface builder there is property of UIButton under Accessibility that says "Plays Sound".
Can any one explain what is this. Actually i am making an application which play sound on every button click and i can disable sounds from setting screen. Will this property of UIButton can help me?
Thanks
You can use [[UIDevice currentDevice] playInputClick]; to play the keyboard input click sound which is available in UIDevice. Check this apple documentation for more details.
You need to do the following for this,
Adopt the UIInputViewAudioFeedback protocol in your input view class.
Implement the enableInputClicksWhenVisible delegate method to return YES.
Do this in UIView class,
#interface MyView : UIView <UIInputViewAudioFeedback>
Then implement enableInputClicksWhenVisible method
- (BOOL)enableInputClicksWhenVisible
{
return YES;
}
If you are facing issues with this, you can check this.
The answer to your original question of whether you can use the Plays Sound 'property' (it's probably not actually a property) to make your buttons play sound is: No. Plays Sound is a 'Trait' that describes the Accessibility of an object (in this case a button) for people who are using VoiceOver (i.e. most likely people who are blind). You can read the documentation here.
hey you want to play sound on button click event then use bellow code also..
-(IBAction)playSound:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"adriantnt_release_click" ofType:#"mp3"]; /// set .mp3 name which you have in project
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
and use like bellow..
-(IBAction)yourButton_Clicked:(id)sender
{
[self performSelector:#selector(playSound:) withObject:sender];
//your code write here
}
Note: Add AudioToolbox.framework Framework And also import
#import<AVFoundation/AVAudioPlayer.h> in .h file and also add Delegate AVAudioPlayerDelegate in .h file
i hope this help you...

adding a complex animation without using UIImage

i want to create a childrens book like this one:
http://www.youtube.com/watch?v=Sc1APRfP2o8
and i wanted to know, How do i create the animations? I have searched and all i can find are creating animations via putting together UIImages and then playing them. But i sure hope i do not have to do this with my whole book. I was wondering if there is another program i could use to create the animation in and then import it into Xcode and then just call it to play. Is this possible? Thanks a bunch!
Check out Cocoas2D.
I didn't watch the video but if you want to create an animation easily aybe you can try gif
NSData *gif = [NSData dataWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"filename" ofType:#"gif"]];
UIWebView *view = [[UIWebView alloc] initWithFrame:frame];
[view loadData:gif MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
this might work

iPhone - user interaction with programmatically added UIImageView

So I'm trying to add UIImageViews programatically (in this case I don't have the option of doing it in IB) and I want to be able to refer to them and manipulate them in the -touchesBegan and -touchesMoved methods.
I've added the images like this:
UIImageView *newPiece = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", [piece tag]]]];
newPiece.frame = CGRectMake(pieceX, pieceY, pieceW, pieceH);
[newPiece setCenter:CGPointMake(pieceX, pieceY)];
[newPiece setTag:[piece tag]];
[[self view] addSubview:newPiece];
[newPiece release];
And note, many of these newPiece's are added programmatically, because the method that this is in is called more than once, so the images have different centers and images and stuff, so would I need an array to hold all of them?
Thanks
NSMutableArray would probably suit your needs.
Check this very detailed post. It's precisely about handling several programatically added UIImageViews and it worked nice for me.
Create multiple views and make the touched view follow the users touch
Best luck.