I'm working on my project in Xcode writing in Objective C & I like to know, is there a way to duplicate files? I keep looking it up on the internet & in my books, I can't fond anything. Whenever I try to #include, it doesn't seem to work.
This is what I have so far, this is the .h file.
#import <UIKit/UIKit.h>
#interface ViewController :UIViewController {
}
- (IBAction)link;
#end
#include <UIKit/UIKit.h>
#interface ViewController2 :UIViewController {
}
- (IBAction)link;
#end
This is the /m file which doesn't want to work.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)link {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"https://twitter.com/sexybeast914"]];
}
#end
#include "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)link {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"https://twitter.com/sexybeast914"]];
}
#end
I always get the same to errors, "Cannot declare class extension for "ViewController", & "Reimplementation of class "ViewController"
If someone could help, it would be nice.
Thanks, :)
You are declaring the class ViewController twice. You can't have two classes of the same name.
I'm betting that second copy of ViewController was meant to be ViewController2 and you forgot to rename it in the file after copy/pasting the code from the first file?
Related
I am trying to follow the Viper pattern in a small objc project. I get the different roles of each part with no particular issue.
However, the part I have an issue with is when I try to move the delegate/datasource of my tableview to another file, because I read that this is how it's supposed to be done.
I followed that post: iOS using VIPER with UITableView but I can't manage to compile.
This issue here is that I have no idea on how to make an extension in Objc. I tried a lot of syntaxes, but none of them worked.
How (by example) would I properly have in VIPER "MyViewController.m/h" & "MyTableViewController.m/h" where "MyTableViewController" is an extension of "MyViewController"?
Meaning that we would see <UITableViewDelegate> in "MyViewController.h".
Thanks a lot for your help. This is probably a redundant question but I didn't manage to find a clear answer, if any, to my extension issue.
Thanks to #Kamil.S in the comment above, I manage to find what I wanted in the apple documentation!
Indeed, extensions in Objc are called "Categories". I pretty much did what was written on the post I linked in my original question.
So here's a simplified example if anyone needs it:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) id<ViewToPresenterProtocol> presenter;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
// All my code, ViewDidLoad, and so on
#end
CollectionViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface ViewController (CollectionViewController) <UICollectionViewDelegate, UICollectionViewDataSource>
#end
CollectionViewController.m
#import <UIKit/UIKit.h>
#import "CollectionViewController.h"
#import "ViewController.h"
#implementation ViewController (CollectionViewController)
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.presenter getNumberOfItems];
}
// ...
// Here add others functions for CollectionView Delegate/Datasource protocols
#end
I have an app that has three views. All three views have an Ad Banner at the bottom of the screen. The first view creates an audio streamer which is paused when the Ad Banner on this view is clicked. I'm trying to use the AdBanner delegate methods on the second view to stop/start the audio. When the Ad Banner is selected the AdBanner delegate methods should call my custom delegate functions. The code compiles and runs but doesn't function correctly.
Using NSLog I've determined that the Ad Banner is calling its delegate function correctly but this isn't calling the custom delegate.
Hope this makes sense. Any help would be appreciated. Here is my code.
SecondViewControler H-file
#import "SecondViewController.h"
#protocol demoViewControllerDelegate <NSObject>
#required
-(void)stopSent;
-(void)startSent;
#end
#interface SecondViewController ()
{
id<demoViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id<demoViewControllerDelegate> delegate;
#end
SecondViewController M-file
#implementation SecondViewController
#synthesize delegate;
Protocols
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
[delegate stopSent];
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner{
[delegate startSent];
}
FirstViewController H-file
#import <QuartzCore/QuartzCore.h>
#import "iAd/iAd.h"
#import <MessageUI/MessageUI.h>
#import "AudioStreamer.h"
#import "Reachability.h"
#import "SecondViewController.h"
#import "MFAppDelegate.h"
#import "MFSideMenu.h"
Class secondViewConroller;
#interface DemoViewController : UIViewController <ADBannerViewDelegate,demoViewControllerDelegate> {
}
#end
FirstViewController M-file
-(void)stopSent{
if (isPlaying) {
[streamer stop];
wasPlaying=true;
}
}
-(void)startSent{
if (wasPlaying) {
[streamer start];
isPlaying=true;
}
}
Your protocol methods need to be implemented in the class that you've designated as your delegate target.
It looks like your DemoViewController (or FirstViewController) is the object you've designated as the delegate, since you've given the interface the "<ADBannerViewDelegate,demoViewControllerDelegate>" designations.
Then, from your Second View Controller, you can call the object you designated and set as a delegate by doing:
[delegate startSent];
and
[delegate stopSent];
in the appropriate locations, which appear to be "bannerViewActionShouldBegin" and "bannerViewActionDidFinish", respectively.
You should also make sure that the delegate is properly set, therefore instead of:
[delegate startSent];
you should actually do this:
if(delegate)
[delegate startSent];
else
NSLog( #"delegate is null; we should figure out why" );
i am making an Mac OS X application and i'm trying to let classes know of each other
Controller creates View1 and View2
BaseView has a property of Controller
View1 and View2 extends from BaseView
here is my example
Controller Class
#import <Cocoa/Cocoa.h>
#import "View1.h"
#class View1;
#interface Controller : NSViewController
{
View1 *_view1;
}
#end
//////
#import "Controller.h"
#implementation Controller
- (id) init
{
self = [super init];
if( self )
{
_view1 = [[View1 alloc] initWithFrame:CGRectZero];
_view1.controller = self;
}
return self;
}
#end
BaseView Class
#import <Cocoa/Cocoa.h>
#import "Controller.h"
#class Controller;
#interface BaseView : NSView
{
Controller *_controller;
}
#property (nonatomic,assign) Controller *controller;
#end
//////
#import "BaseView.h"
#implementation BaseView
#synthesize controller = _controller;
#end
View Example Class
#import "BaseView.h"
#interface View1 : BaseView
#end
//////
#import "View1.h"
#implementation View1
#end
But it gives me this error:
Controller.m:23:16: Property 'controller' cannot be found in forward class object 'View1'
what do i do wrong?
When you use a forward declaration in your header file(e.g. #class View1;), you do not need to #import the header.
In your View1.h you don't declare a #class, that's where you get the error. Nevertheless I suggest you to use forward declaration in your header file and import the needed headers in your implementation file when you need the method declarations etc. - this will prevent you from a header loop, too.
your code should look like
#class BaseView;
#interface View1 : BaseView
#end
//////
#import "View1.h"
#import "BaseView.h"
#implementation View1
#end
I think the problem might not be in your code.
Are they in the same relative folder? Is BaseView in a different location? You might have to go to your project settings and adjust your User Header Search Paths if this is the case
Also, make sure that all of your files are set up for the target you are currently running, by clicking on them on the project navigator, and in the right-hand side menu check which targets it's marked
My program was running fine, but I changed something and now it has over 48 errors.
I think I know the problem, but I don't know how to fix it. I created a class called mViewBase for all my UIViewControllers to derive from.
I decided to have a navigtion bar at the bottom of all my views, to go to other view controllers called cakes2. So cakes2.h imports mViewBase, and mViewBase import cakes2.h
You must be able to do this in Objective-C. Does anybody have any idea of what I can do?
My mViewBase.h file:
#import <UIKit/UIKit.h>
#import "Cakes2.h"
#interface mViewBase : UIViewController {
UIView *mBackground;
UIView *mBackArrow;
UITextView *mTitle;
// Cakes2 *mCakes;
}
-(void) aSetTitle: (NSString *) NewTitle;
-(IBAction) aBack: (id) tender;
-(IBAction) aHome: (id) sender;
-(IBAction) aCakes: (id) sender;
-(IBAction) aCall: (id) sender;
-(IBAction) aDirections: (id) sender;
#end
My Cakes2.h file:
#import <UIKit/UIKit.h>
#import "Gallery.h"
#import "WebView.h"
#import "mViewBase.h" // Circular reference! But I need it
#interface Cakes2 : mViewBase <UITableViewDelegate, UITableViewDataSource> {
// Gallery *mGallery;
IBOutlet UITableView *mMenu;
// WebView *mWebView;
}
-(IBAction) aOpenWeb;
#end
You can use a forward declaration in one of your header files to avoid the need to import the other header. For example, in mViewBase.h, you can say:
#class Cakes2;
Now the compiler knows that "Cakes2" refers to a class, and you don't need to import the entire Cakes2.h file.
I think you should perhaps consider using a UITabBarController. It is made specifically for managing several view controllers from a bar at the bottom of the screen.
Here I am fighting with a problem couple of hours.
My problem is
Here is my code in appdelegate didFinishLaunching method.
#import "CorkItAppDelegate.h"
#implementation CorkItAppDelegate
#synthesize window,isPicker,isFirstTime,winTyp,winTypId,wineCatName,wineRegName,theViewController,catId,regId,facebookObject,isGetWineName,getNewWineName,isNewWine,getNewWineReg,isNewReg,wineDetPk,currentLocation,internetConnectionStatus;
static NSString* mapCurrentLocationUrlArg = #"http://maps.google.com/maps?q=%s#";
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Override point for customization after application launch
//facebookObject=[[FacebookMyLib alloc]init];
[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDataStructures];
[MyCLController sharedInstance].delegate = self;
[[MyCLController sharedInstance].locationManager startUpdatingLocation];
isGetWineName = NO;
facebookObject = [[FacebookMyLib alloc] init];
theViewController = [[[MasterViewController alloc] init]autorelease];
UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:theViewController];
controller.navigationBar.barStyle = UIBarStyleBlackOpaque;
[window addSubview:controller.view];
[window makeKeyAndVisible];
}
here in this code I declared the MasterView controller globally in .h class. and it written property and synthesize for that.
But when I run the build I am getting two errors at #import "MasterViewController.h" like
error:expected specifier-qualifier-list before 'MasterViewController.h'.
I got two errores they are at:
In my MasterViewController.m
the errors are genereated at #import"MasterViewController.h"
#import "MasterViewController.h"
//here I am getting 2 error:expected specifier-qualifier-list before 'MasterViewController.h'.
#import "CorkItAppDelegate.h"
#import"InfoViewController.h"
#implementation MasterViewController
And the code in CorkItAppDelegate.h is:
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "wineDetails.h"
#import "wineTypes.h"
#import "WineCategoriesList.h"
#import "MyCLController.h";
#import "Reachability.h";
#import "WineRegion.h"
#import "EventsList.h"
#import"FBConnect/FBConnect.h"
#import"FacebookMyLib.h"
#import "MasterViewController.h"
#interface CorkItAppDelegate : NSObject <UIApplicationDelegate,MyCLControllerDelegate> {
MasterViewController* theViewController ;
UIWindow *window;
sqlite3 *database;
NSMutableArray* masterViewList;
}
#property(nonatomic,retain)MasterViewController* theViewController ;
#end
can anyone suggest me how to get rid of this.
Anyone's help will be much Appreciated.
Thank you,
Monish.
Remove all the headers except the sdk headers like
remove:
#import "wineDetails.h"
#import "wineTypes.h"
#import "WineCategoriesList.h"
#import "MyCLController.h";
#import "Reachability.h";
#import "WineRegion.h"
#import "EventsList.h"
#import"FacebookMyLib.h"
#import "MasterViewController.h"
Don't remove :
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import"FBConnect/FBConnect.h"
Hopes this helps